<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://thechange.wiki/index.php?action=history&amp;feed=atom&amp;title=Code%3Aconvert-nasa-population.c</id>
	<title>Code:convert-nasa-population.c - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://thechange.wiki/index.php?action=history&amp;feed=atom&amp;title=Code%3Aconvert-nasa-population.c"/>
	<link rel="alternate" type="text/html" href="https://thechange.wiki/index.php?title=Code:convert-nasa-population.c&amp;action=history"/>
	<updated>2026-04-30T20:23:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://thechange.wiki/index.php?title=Code:convert-nasa-population.c&amp;diff=303&amp;oldid=prev</id>
		<title>Elie: Created page with &quot;Converts NASA GPWv4 population data from its original form (:File:gpw-v4-population-count-rev11_2020_2pt5_min_asc.zip) to a headerless form (:File:population.data-float64-8640x4320)  Instructions: * Extract &quot;gpw_v4_population_count_rev11_2020_2pt5_min.asc&quot; from :File:gpw-v4-population-count-rev11_2020_2pt5_min_asc.zip, into your &quot;data/&quot; subfolder. * Run the program (code is below) * The :File:population.data-float64-8640x4320 will appear in your &quot;data/&quot; s...&quot;</title>
		<link rel="alternate" type="text/html" href="https://thechange.wiki/index.php?title=Code:convert-nasa-population.c&amp;diff=303&amp;oldid=prev"/>
		<updated>2022-08-30T12:25:49Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Converts NASA GPWv4 population data from its original form (&lt;a href=&quot;/File:gpw-v4-population-count-rev11_2020_2pt5_min_asc.zip&quot; title=&quot;File:gpw-v4-population-count-rev11 2020 2pt5 min asc.zip&quot;&gt;File:gpw-v4-population-count-rev11_2020_2pt5_min_asc.zip&lt;/a&gt;) to a headerless form (&lt;a href=&quot;/File:population.data-float64-8640x4320&quot; title=&quot;File:population.data-float64-8640x4320&quot;&gt;File:population.data-float64-8640x4320&lt;/a&gt;)  Instructions: * Extract &amp;quot;gpw_v4_population_count_rev11_2020_2pt5_min.asc&amp;quot; from &lt;a href=&quot;/File:gpw-v4-population-count-rev11_2020_2pt5_min_asc.zip&quot; title=&quot;File:gpw-v4-population-count-rev11 2020 2pt5 min asc.zip&quot;&gt;File:gpw-v4-population-count-rev11_2020_2pt5_min_asc.zip&lt;/a&gt;, into your &amp;quot;data/&amp;quot; subfolder. * Run the program (code is below) * The &lt;a href=&quot;/File:population.data-float64-8640x4320&quot; title=&quot;File:population.data-float64-8640x4320&quot;&gt;File:population.data-float64-8640x4320&lt;/a&gt; will appear in your &amp;quot;data/&amp;quot; s...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Converts NASA GPWv4 population data from its original form ([[:File:gpw-v4-population-count-rev11_2020_2pt5_min_asc.zip]]) to a headerless form ([[:File:population.data-float64-8640x4320]])&lt;br /&gt;
&lt;br /&gt;
Instructions:&lt;br /&gt;
* Extract &amp;quot;gpw_v4_population_count_rev11_2020_2pt5_min.asc&amp;quot; from [[:File:gpw-v4-population-count-rev11_2020_2pt5_min_asc.zip]], into your &amp;quot;data/&amp;quot; subfolder.&lt;br /&gt;
* Run the program (code is below)&lt;br /&gt;
* The [[:File:population.data-float64-8640x4320]] will appear in your &amp;quot;data/&amp;quot; subfolder.&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Converts NASA GPWv4 data (population count) from ASCII format to binary data (64-bit double-precision floating point 2D array)&lt;br /&gt;
// To compile:  gcc convert-nasa-population.c -O -o convert-nasa-population&lt;br /&gt;
&lt;br /&gt;
#define IN_FILE  &amp;quot;data/gpw_v4_population_count_rev11_2020_2pt5_min.asc&amp;quot;&lt;br /&gt;
#define OUT_FILE &amp;quot;data/population.data-float64-8640x4320&amp;quot;&lt;br /&gt;
#define INPUT_ASCII_DATA_START 131&lt;br /&gt;
#define INPUT_WIDTH 8640&lt;br /&gt;
#define INPUT_HEIGHT 4320&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
 FILE* in = fopen(IN_FILE, &amp;quot;r&amp;quot;);&lt;br /&gt;
 if (in == NULL) {&lt;br /&gt;
  perror(IN_FILE);&lt;br /&gt;
  return 1;&lt;br /&gt;
 }&lt;br /&gt;
 if (fseek(in, INPUT_ASCII_DATA_START, SEEK_SET)) {&lt;br /&gt;
  perror(IN_FILE);&lt;br /&gt;
  return 2;&lt;br /&gt;
 }&lt;br /&gt;
 FILE* out = fopen(OUT_FILE, &amp;quot;w&amp;quot;);&lt;br /&gt;
 if (out == NULL) {&lt;br /&gt;
  perror(OUT_FILE);&lt;br /&gt;
  return 3;&lt;br /&gt;
 }&lt;br /&gt;
 printf(&amp;quot;Input:  %s\nOutput: %s\n&amp;quot;,IN_FILE,OUT_FILE);&lt;br /&gt;
 int n=0;&lt;br /&gt;
 double value=0;&lt;br /&gt;
 while (!feof(in)) {&lt;br /&gt;
  if (fscanf(in, &amp;quot;%lf&amp;quot;, &amp;amp;value) == 0) break;&lt;br /&gt;
  // if (!n) printf(&amp;quot;first pixel value: %lf\n&amp;quot;, value);&lt;br /&gt;
  if (value &amp;lt; 0) value = 0;&lt;br /&gt;
  if (fwrite(&amp;amp;value, sizeof(double), 1, out) == 0) break;&lt;br /&gt;
  ++n; if (!(n &amp;amp; 131071)) printf(&amp;quot;wrote %d MiB\n&amp;quot;, (n&amp;gt;&amp;gt;17));&lt;br /&gt;
 }&lt;br /&gt;
 fclose(in);&lt;br /&gt;
 fclose(out);&lt;br /&gt;
 printf(&amp;quot;wrote %d numbers to %s\n&amp;quot;, n, OUT_FILE);&lt;br /&gt;
 return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Elie</name></author>
	</entry>
</feed>