most.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933
  1. /*
  2. MOST - Model Starter
  3. ---------------------------------------------------------------------------
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. ---------------------------------------------------------------------------
  16. */
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include <X11/Xlib.h>
  25. #include <X11/Xutil.h>
  26. #include <X11/Xatom.h>
  27. #include <X11/keysym.h>
  28. #include <X11/XKBlib.h>
  29. #define INT int
  30. #define LINEMAX 256
  31. typedef char String[LINEMAX];
  32. String Buffer;
  33. #ifndef MIN
  34. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  35. #endif
  36. #ifndef MAX
  37. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  38. #endif
  39. #define FULLARC (360 * 64)
  40. #define WINDOW_WIDTH 820
  41. /* Models */
  42. #define PUMA 0
  43. #define SAM 1
  44. #define CAT 2
  45. #define PLASIM 3
  46. #define MODELS 4
  47. /* Resolutions */
  48. #define RES_T15 0
  49. #define RES_T21 1
  50. #define RES_T31 2
  51. #define RES_T42 3
  52. #define RES_T63 4
  53. #define RESOLUTIONS 5
  54. int PlasimSteps[RESOLUTIONS] =
  55. {
  56. 60, // T15
  57. 45, // T21
  58. 30, // T31
  59. 20, // T42
  60. 10 // T63
  61. };
  62. int PumaSteps[RESOLUTIONS] =
  63. {
  64. 60, // T15
  65. 60, // T21
  66. 45, // T31
  67. 30, // T42
  68. 15 // T63
  69. };
  70. int ResLat[RESOLUTIONS] =
  71. {
  72. 24,
  73. 32,
  74. 48,
  75. 64,
  76. 96
  77. };
  78. int Resolution = -1;
  79. char *ShortModelName[MODELS] =
  80. {
  81. "puma",
  82. "sam",
  83. "cat",
  84. "plasim"
  85. };
  86. char *FullModelName[MODELS] =
  87. {
  88. "PUMA",
  89. "SAM",
  90. "CAT",
  91. "Planet Simulator"
  92. };
  93. int Model = PUMA;
  94. #define ALL -1
  95. #define EARTH 0
  96. #define MARS 1
  97. #define EXO 2
  98. #define PLANETS 3
  99. char *PlanetName[PLANETS] =
  100. {
  101. "Earth",
  102. "Mars",
  103. "Exo"
  104. };
  105. int Planet = EARTH;
  106. double *OroEarth; // Earth Orography T42 (128 x 64)
  107. double *OroMars; // Mars Orography T42 (128 x 64)
  108. double *OroPrep; // Preprocessed Orography
  109. Pixmap OpmEarth;
  110. Pixmap OpmMars;
  111. Pixmap OpmPrep;
  112. double RevGra = 1.0 / 9.81;
  113. /* Object types */
  114. #define SEL_TEXT 1
  115. #define SEL_CHECK 2
  116. #define SEL_INT 3
  117. #define SEL_REAL 4
  118. #define SEL_TEVA 5
  119. #define SEL_PLANET 6
  120. struct ItemStruct
  121. {
  122. char list[80]; // Name of namelist
  123. char name[80]; // Name of variable
  124. char text[80]; // Text value
  125. float pvec[PLANETS]; // Planetary parameter
  126. double rval; // Real value
  127. int ival; // Integer value
  128. int model; // Model
  129. int flag; // Flag
  130. };
  131. struct SelStruct
  132. {
  133. struct SelStruct *Next; // Link to next Sel
  134. struct SelStruct *Prev; // Link to previous Sel
  135. struct ItemStruct *Item; // Link to item
  136. int type; // Type (TEXT,CHECK,INT,REAL)
  137. int x; // x coordinate of box
  138. int y; // y coordinate of box
  139. int h; // height of box
  140. int w; // width of box
  141. int xo; // x offset
  142. int xt; // x coordinate for text
  143. int yt; // y coordinate for text
  144. int lt; // length of text
  145. int iv; // integer value of box content
  146. int div; // default integer value
  147. int no; // 1: not selectable
  148. int teco; // text colour
  149. int edco; // edit column (cursor)
  150. int hide; // 1: don't show
  151. float fv; // floating value of box content
  152. float dfv; // default floating value
  153. float fpl[PLANETS]; // planet dependent floats
  154. int *piv; // pointer to linked integer variable
  155. float *pfv; // pointer to linked float variable
  156. char text[80]; // text to display
  157. char teva[16]; // box content
  158. };
  159. struct SelStruct SelStart;
  160. struct SelStruct SelModels[MODELS];
  161. struct SelStruct *CursorSel;
  162. struct SelStruct *ComEnd;
  163. struct SelStruct *SelAno;
  164. struct SelStruct *SelTgr;
  165. struct SelStruct *SelOro;
  166. struct SelStruct *SelMod;
  167. struct SelStruct *SelRes;
  168. struct SelStruct *SelOce;
  169. struct SelStruct *SelLsg;
  170. struct SelStruct *SelIce;
  171. struct SelStruct *SelAnn;
  172. struct SelStruct *SelPlanet[PLANETS];
  173. struct SelStruct *SelSYear;
  174. struct SelStruct *SelCPU;
  175. struct SelStruct *SelMulti;
  176. struct SelStruct *SelLat2;
  177. // Title lines for the entry of resolution
  178. char *DimText1;
  179. char *DimText2;
  180. char *DimText3;
  181. #define DIMLOGO 4
  182. struct SymbolStruct
  183. {
  184. int x; // x pos
  185. int y; // y pos
  186. int w; // width
  187. int h; // height
  188. int b; // background color
  189. int f; // foreground color
  190. char **i; // image
  191. char *d; // image data
  192. XImage *X; // X-Image
  193. char t[2][80]; // caption
  194. } Logo[DIMLOGO];
  195. int Logos;
  196. struct ColorStrip
  197. {
  198. double Lo;
  199. double Hi;
  200. char *Name;
  201. unsigned long pixel;
  202. };
  203. unsigned long WinBG = 0; // Black background
  204. unsigned long TextC = 0; // Text color
  205. unsigned long HeadC = 0; // Heading color
  206. #define DIMFRAME 3
  207. int Frames;
  208. struct FrameStruct
  209. {
  210. void (*Action)();
  211. Pixmap pixmap; // pixmap
  212. int x; // x pos
  213. int y; // y pos
  214. int w; // width
  215. int h; // height
  216. int b; // background color
  217. int f; // foreground color
  218. int xs; // x pos selection
  219. int ys; // y pos selection
  220. int ws; // width selection
  221. int hs; // height selection
  222. char t[3][80]; // text
  223. } Frame[DIMFRAME];
  224. int FrameNo;
  225. int Buttons;
  226. int Button1Down = 1;
  227. #define DIMBUTTON 5
  228. struct ButtonStruct
  229. {
  230. void (*Action)();
  231. int x; // x pos
  232. int y; // y pos
  233. int w; // width
  234. int h; // height
  235. int b; // background color
  236. int f; // foreground color
  237. char t[3][80]; // text
  238. } Button[DIMBUTTON];
  239. int Buttons;
  240. /* Coordinates */
  241. int opbox_x;
  242. int opbox_y;
  243. int opbox_w;
  244. int opbox_h;
  245. int nlbox_x;
  246. int nlbox_y;
  247. int nlbox_w;
  248. int nlbox_h;
  249. int nlpos_x;
  250. int nledi_x;
  251. int sfbox_x;
  252. int sfbox_y;
  253. int sfbox_w;
  254. int sfbox_h;
  255. int sfbox_b;
  256. int bubox_x;
  257. int bubox_y;
  258. int bubox_w;
  259. int bubox_h;
  260. int CursorCol;
  261. int LastSel;
  262. int EdiKeyCode;
  263. #define NL_MAX_ITEMS 100
  264. int NL_items = 0;
  265. struct ItemStruct NL_list[NL_MAX_ITEMS];
  266. int BigEndian;
  267. int Oce;
  268. int Ice;
  269. int Lsg = 1;
  270. int SimStart;
  271. int SimYears;
  272. int nreadsr;
  273. int noutput;
  274. int ndebug;
  275. int nprec;
  276. int ngui;
  277. int noro = 1;
  278. int OroAno;
  279. int OroClear;
  280. int OroAqua;
  281. int TgrAno;
  282. int nac;
  283. int Preprocessed;
  284. int SAMindex;
  285. int ScreenHeight;
  286. int Expert = 0;
  287. int CatEnabled;
  288. int SamEnabled;
  289. int LsgEnabled;
  290. int ModeRadiusSq;
  291. int ForceRebuild;
  292. int dxsh;
  293. int dxs2;
  294. int Yoden; // PUMA setup for Yoden experiment
  295. int CatSim = 51; // simulation setup number for CAT
  296. /* Special parameter */
  297. int Latitudes = 32; // Number of latitudes in atmosphere
  298. int Latitude2 = 32; // Number of latitudes for 2nd. instance
  299. int Levels = 10; // Number of levels in atmosphere
  300. int Cores = 1; // Number of cores for parallel version
  301. int Multirun = 1; // Number of coupled runs
  302. int MultirunEnabled = 0; // Multirun module present?
  303. int Truncation = 21; // Spectral truncation computed from Latitudes
  304. unsigned Seed;
  305. char cfg_file[256] = "most_last_used.cfg";
  306. char exec_name[256] = "most_puma.x";
  307. char exec_nam2[256] = "most_puma.x"; // 2nd. instance
  308. char exec_ppp[256] = "most_ppp.x";
  309. char oro_name[256] = "N064_surf_0129.sra"; // Orography in T42 resolution
  310. char namelist_name[256] = "puma_namelist";
  311. char diag_name[256] = "puma_diag";
  312. char outp_name[256] = "puma_output";
  313. char build_name[256] = "most_puma_build";
  314. char build_ppp[256] = "most_ppp_build";
  315. char run_name[256] = "most_puma_run";
  316. char run_ppp[256] = "most_ppp_run";
  317. char res_name[256] = "resmod.f90";
  318. char hostname[256] = "localhost";
  319. char mpirun[256] = "mpirun"; // "openmpirun" for Open MPI
  320. int ScreenN;
  321. time_t CurrentDate;
  322. Display *display;
  323. unsigned int ScreenW,ScreenH;
  324. int SmallScreen = 1;
  325. static char *progname = "Most";
  326. int DimX;
  327. int DimY;
  328. int DimTr = 21;
  329. int DimSH = 11 * 23;
  330. int DimSE = 11 * 23 + 2;
  331. int ScreenD;
  332. int OffX;
  333. int OffY = 0;
  334. int WinXSize;
  335. int WinYSize;
  336. int WhitePix;
  337. int BlackPix;
  338. int *Ampli;
  339. int *ModeX;
  340. int *ModeY;
  341. int *ModeM;
  342. int *ModeN;
  343. Colormap colormap;
  344. XColor xcolor1,xcolor2;
  345. XColor Red,Green,Blue,Grey,LightRed,DarkRed,LightBlue,DarkBlue;
  346. XColor LightGreen,DarkGreen,Yellow,Cyan,Dummy;
  347. Window Cow; // Control bar
  348. XTextProperty WinconName1;
  349. XEvent WinEvent;
  350. XSizeHints CowSizeHints;
  351. int count;
  352. XEvent report;
  353. GC gc;
  354. char ModFontName[80] = "-misc-fixed-bold-r-normal--15-*-*-*-*-*-*-*";
  355. char FixFontName[80] = "-misc-fixed-bold-r-normal--15-*-*-*-*-*-*-*";
  356. char BigFontName[80] = "-misc-fixed-medium-r-normal--20-*-*-*-*-*-*-*";
  357. //char ModFontName[80] = "9x15bold";
  358. //char FixFontName[80] = "9x15bold";
  359. //char BigFontName[80] = "10x20";
  360. XFontStruct *ModFont;
  361. XFontStruct *FixFont;
  362. XFontStruct *BigFont;
  363. XFontStruct *SamFont;
  364. int ModFontHeight;
  365. int FixFontHeight;
  366. int BigFontHeight;
  367. int ModFontWidth;
  368. int FixFontWidth;
  369. int BigFontWidth;
  370. int ModFontAscent;
  371. int FixFontAscent;
  372. int BigFontAscent;
  373. int EdiFirstKey;
  374. int EdiLastKey;
  375. int EdiSymsPerKey;
  376. int NumLockMask;
  377. int ModeSwitchMask;
  378. int Debug = 0;
  379. KeySym *EdiKeymap;
  380. XModifierKeymap *Mok;
  381. char *display_name = NULL;
  382. XWMHints wm_hints;
  383. XClassHint class_hints;
  384. Atom Delwin;
  385. char *mona[12] =
  386. {
  387. "Jan","Feb","Mar",
  388. "Apr","May","Jun",
  389. "Jul","Aug","Sep",
  390. "Oct","Nov","Dec"
  391. };
  392. char datch[32];
  393. struct MapImageStruct
  394. {
  395. char *d; // Bitmap data
  396. int w; // Image width
  397. int h; // Image height
  398. int f; // Rotation factor
  399. float l; // Reference longitude
  400. float r; // Rotation speed [deg/step]
  401. XImage *X; // XImage structure
  402. };
  403. struct MapImageStruct MapHRE; // Hires (2560x1280) Earth image
  404. struct MapImageStruct MapHRM; // Hires ( ) Mars image
  405. struct MapImageStruct MapLRE; // Lores azimuthal Earth image
  406. struct MapImageStruct MapLRM; // Lores azimuthal Mars image
  407. struct MapImageStruct MapLRK; // Lores azimuthal Kepler-16
  408. struct MapImageStruct MapLRL; // Lores azimuthal Kepler-186b
  409. /* Isoarea data */
  410. #define TOLELO 0x0001
  411. #define TOLEHI 0x0002
  412. #define TORILO 0x0004
  413. #define TORIHI 0x0008
  414. #define BOLELO 0x0010
  415. #define BOLEHI 0x0020
  416. #define BORILO 0x0040
  417. #define BORIHI 0x0080
  418. #define TOLEIN 0x0100
  419. #define TOREIN 0x0200
  420. #define BOLEIN 0x0400
  421. #define BOREIN 0x0800
  422. #define IPX(l,m,h) (VGAX * (x + (l-m) / (l-h)))
  423. #define IPY(l,m,h) (VGAY * (y + (l-m) / (l-h)))
  424. int nlon = 128;
  425. int nlat = 64;
  426. int nlev = 5;
  427. int *Flag;
  428. double VGAX;
  429. double VGAY;
  430. Pixmap pix;
  431. /* Orography units are [m2/s2] */
  432. struct ColorStrip OroStrip[] =
  433. {
  434. {-99999.0, 80.0,"DarkBlue"},
  435. { 80.0, 800.0,"green"},
  436. { 800.0, 1600.0,"brown"},
  437. { 1600.0, 2400.0,"orange"},
  438. { 2400.0,99999.0,"white"},
  439. { 0.0, 0.0,NULL}
  440. };
  441. struct ColorStrip OroMarsStrip[] =
  442. {
  443. {-99999.0,-16000.0,"yellow"},
  444. {-16000.0,-12000.0,"RoyalBlue4"},
  445. {-12000.0, -8000.0,"RoyalBlue3"},
  446. { -8000.0, -4000.0,"RoyalBlue2"},
  447. { -4000.0, 0.0,"RoyalBlue1"},
  448. { 0.0, 4000.0,"dark green"},
  449. { 4000.0, 8000.0,"light green"},
  450. { 8000.0, 12000.0,"brown"},
  451. { 12000.0, 16000.0,"orange"},
  452. { 16000.0, 99999.0,"white"},
  453. { 0.0, 0.0,NULL}
  454. };
  455. struct ColorStrip GibbStrip[] =
  456. {
  457. {-99999.0, 0.0,"DarkBlue"},
  458. { 0.0,99999.0,"green"},
  459. { 0.0, 0.0,NULL}
  460. };
  461. struct ColorStrip TStrip[] =
  462. {
  463. {-99.0,-15.0,"RoyalBlue4"},
  464. {-15.0,-20.0,"RoyalBlue3"},
  465. {-10.0, -5.0,"RoyalBlue2"},
  466. { -5.0, -0.1,"RoyalBlue1"},
  467. { -0.1, 0.1,"yellow"},
  468. { 0.1, 5.0,"IndianRed1"},
  469. { 5.0, 10.0,"IndianRed2"},
  470. { 10.0, 15.0,"IndianRed3"},
  471. { 15.0, 20.0,"IndianRed4"},
  472. { 20.0,999.0,"red"},
  473. { 0.0, 0.0,NULL}
  474. };
  475. void DoNothing(void)
  476. {
  477. printf("Do nothing\n");
  478. }
  479. void ChangeModel(int NewMo)
  480. {
  481. int i;
  482. struct SelStruct *Sel;
  483. if (NewMo < 0) // locate active model
  484. {
  485. NewMo = PUMA; // default if none is specified
  486. for (i=0 , Sel = SelMod ; i < MODELS; ++i , Sel = Sel->Next)
  487. {
  488. if (Sel->iv == 1) NewMo = i;
  489. }
  490. }
  491. ComEnd->Next = &SelModels[NewMo];
  492. ComEnd->Next->Prev = ComEnd;
  493. for (i=0 , Sel = SelMod ; i < MODELS; ++i , Sel = Sel->Next)
  494. {
  495. if (i == NewMo) Sel->iv = 1;
  496. else Sel->iv = 0;
  497. }
  498. if (NewMo == PUMA || NewMo == SAM)
  499. {
  500. SelAno->hide = 0;
  501. SelAnn->no = 0;
  502. SelOce->no = 1;
  503. SelIce->no = 1;
  504. SelOro->no = 0;
  505. SelSYear->no = 1;
  506. if (SelLsg) SelLsg->no = 1;
  507. for (i=0 ; i < PLANETS ; ++i) SelPlanet[i]->no = 1;
  508. }
  509. if (NewMo == PLASIM)
  510. {
  511. SelAno->hide = 1;
  512. SelAnn->no = 1;
  513. SelOro->no = 1;
  514. SelSYear->no = 0;
  515. if (Expert)
  516. {
  517. SelOce->no = 0;
  518. SelIce->no = 0;
  519. if (SelLsg) SelLsg->no = 0;
  520. for (i=0 ; i < PLANETS ; ++i) SelPlanet[i]->no = 0;
  521. }
  522. else
  523. {
  524. SelOce->no = 0;
  525. SelIce->no = 0;
  526. for (i=0 ; i < PLANETS ; ++i) SelPlanet[i]->no = 0;
  527. }
  528. }
  529. if (Expert)
  530. {
  531. if (NewMo == CAT)
  532. {
  533. strcpy(DimText1,"NGX ");
  534. strcpy(DimText2,"NGY ");
  535. }
  536. else
  537. {
  538. strcpy(DimText1,"Latitudes #1 ");
  539. strcpy(DimText2,"Latitudes #2 ");
  540. }
  541. }
  542. else
  543. {
  544. if (NewMo == CAT)
  545. {
  546. strcpy(DimText1,"[ 64 x 64 ]");
  547. strcpy(DimText2,"[ 256 x 256 ]");
  548. strcpy(DimText3,"[1024 x 1024]");
  549. }
  550. else
  551. {
  552. strcpy(DimText1,"T21 [64x32]");
  553. strcpy(DimText2,"T31 [96x48]");
  554. strcpy(DimText3,"T42 [128x64]");
  555. }
  556. }
  557. Model = NewMo;
  558. }
  559. void WriteSettings(void)
  560. {
  561. int mxtl;
  562. char tb[80];
  563. FILE *fp;
  564. struct SelStruct *Sel;
  565. fp = fopen("most_last_used.cfg","w");
  566. if (!fp) return;
  567. fprintf(fp,"[MoSt 17 - configuration file] created %s\n",ctime(&CurrentDate));
  568. mxtl = 0;
  569. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  570. {
  571. if (Sel->type != SEL_TEXT ) mxtl = MAX(mxtl,strlen(Sel->text));
  572. }
  573. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  574. {
  575. if (Sel->type == SEL_TEXT ) fprintf(fp,"\n[%s]\n",Sel->text);
  576. else
  577. {
  578. memset(tb,' ',sizeof(tb));
  579. strncpy(tb,Sel->text,strlen(Sel->text));
  580. tb[mxtl] = 0;
  581. fprintf(fp,"%s = ",tb);
  582. }
  583. if (Sel->type == SEL_CHECK) fprintf(fp,"%6d\n",Sel->iv);
  584. else if (Sel->type == SEL_INT ) fprintf(fp,"%6d\n",Sel->iv);
  585. else if (Sel->type == SEL_TEVA ) fprintf(fp,"%s\n",Sel->teva);
  586. else if (Sel->type == SEL_REAL || Sel->type == SEL_PLANET)
  587. {
  588. if (Sel->type == SEL_PLANET) Sel->fv = Sel->fpl[Planet];
  589. if (fabs(Sel->fv) >= 1.0 && fabs(Sel->fv) < 9000.0) fprintf(fp,"%10.4f\n",Sel->fv);
  590. else fprintf(fp,"%10.4e\n",Sel->fv);
  591. }
  592. }
  593. fclose(fp);
  594. }
  595. void FormatReal(float fv, char *text)
  596. {
  597. if (fv == 0.0 ||
  598. (fv > 0.001 && fv < 99999.0) ||
  599. (fv > -9999.0 && fv < -0.001))
  600. {
  601. sprintf(text,"%11.4f",fv);
  602. if (!strcmp(text+8,"000")) text[ 8] = 0;
  603. else if (!strcmp(text+9,"00" )) text[ 9] = 0;
  604. else if (text[10] == '0') text[10] = 0;
  605. }
  606. else
  607. {
  608. sprintf(text,"%11.4e",fv);
  609. }
  610. }
  611. void UpdateSelections(struct SelStruct *Sel)
  612. {
  613. char text[80];
  614. int selx;
  615. selx = 0;
  616. for (; Sel ; Sel = Sel->Next)
  617. {
  618. if (Sel == ComEnd->Next) selx = 1;
  619. if (selx) Sel->x = nledi_x;
  620. if (Sel->type == SEL_INT )
  621. {
  622. sprintf(text,"%10d",Sel->iv);
  623. strcpy(Sel->teva,text+10-Sel->edco);
  624. if (Sel->piv) *Sel->piv = Sel->iv;
  625. }
  626. if (Sel->type == SEL_REAL || Sel->type == SEL_PLANET)
  627. {
  628. if (Sel->type == SEL_PLANET) Sel->fv = Sel->fpl[Planet];
  629. FormatReal(Sel->fv,text);
  630. strcpy(Sel->teva,text);
  631. }
  632. }
  633. // if (SelLat2) SelLat2->no = (SelMulti->iv != 2) ;
  634. }
  635. void ChangePlanet(int NewPlanet)
  636. {
  637. struct SelStruct *Sel;
  638. SelPlanet[Planet]->iv = 0;
  639. Planet = NewPlanet;
  640. SelPlanet[Planet]->iv = 1;
  641. strcpy(SelModels[PLASIM].text,PlanetName[Planet]);
  642. SelModels[PLASIM].lt = strlen(PlanetName[Planet]);
  643. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  644. {
  645. if (Sel->type == SEL_PLANET)
  646. {
  647. Sel->fv = Sel->fpl[Planet];
  648. }
  649. }
  650. UpdateSelections(ComEnd->Next);
  651. }
  652. int ReadSettings(char *fn)
  653. {
  654. int pl;
  655. char tb[80];
  656. char *eq;
  657. FILE *fp;
  658. struct SelStruct *Sel;
  659. fp = fopen(fn,"r");
  660. if (!fp)
  661. {
  662. printf("\nUsing default configuration\n");
  663. return 0;
  664. }
  665. fgets(tb,sizeof(tb),fp);
  666. if (strncmp(tb,"[MoSt",5))
  667. {
  668. printf("\nFileheader is: %s\n",tb);
  669. printf("Expected : [MoSt ...]\n");
  670. printf("Using default configuration\n");
  671. fclose(fp);
  672. return 0;
  673. }
  674. while (!feof(fp))
  675. {
  676. eq = strchr(tb,'=');
  677. if (eq)
  678. {
  679. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  680. {
  681. if (!strncmp(tb,Sel->text,strlen(Sel->text)))
  682. {
  683. if (!isalnum(tb[strlen(Sel->text)]))
  684. {
  685. if (Sel->type == SEL_REAL || Sel->type == SEL_PLANET)
  686. {
  687. Sel->fv = atof(eq+1);
  688. if (Sel->pfv) *Sel->pfv = Sel->fv;
  689. if (Sel->type == SEL_PLANET) Sel->fpl[Planet] = Sel->fv;
  690. }
  691. else
  692. {
  693. Sel->iv = atoi(eq+1);
  694. if (Sel->piv) *Sel->piv = Sel->iv;
  695. }
  696. }
  697. }
  698. }
  699. }
  700. else if (!strncmp(tb,"[Modules]",9)) ChangeModel(-1);
  701. else
  702. {
  703. for (pl=0 ; pl < PLANETS ; ++pl)
  704. {
  705. if (!strncmp(tb+1,PlanetName[pl],strlen(PlanetName[pl])) && pl != Planet)
  706. ChangePlanet(pl);
  707. }
  708. }
  709. fgets(tb,sizeof(tb),fp);
  710. }
  711. fclose(fp);
  712. return 1;
  713. }
  714. void Exit(void)
  715. {
  716. int i;
  717. WriteSettings();
  718. // XNextEvent(display,&WinEvent);
  719. // XCloseDisplay(display);
  720. exit(0);
  721. }
  722. void AbortMessage(char *s)
  723. {
  724. printf("\n\n%s\n",s);
  725. exit(1);
  726. }
  727. void Abort(void)
  728. {
  729. Exit();
  730. }
  731. void InitNextSelection(struct SelStruct *Sel, int dy, char *t)
  732. {
  733. Sel->type = Sel->Prev->type;
  734. Sel->y = Sel->Prev->y + dy;
  735. Sel->h = Sel->Prev->h;
  736. Sel->w = Sel->Prev->w;
  737. Sel->xo = Sel->Prev->xo;
  738. Sel->yt = Sel->Prev->yt + dy;
  739. strcpy(Sel->text,t);
  740. Sel->lt = strlen(Sel->text);
  741. Sel->teco = Sel->Prev->teco;
  742. Sel->edco = Sel->Prev->edco;
  743. }
  744. void CursorOn(void)
  745. {
  746. int x,y,w,h;
  747. if (CursorSel)
  748. {
  749. x = CursorSel->x + CursorCol * FixFontWidth;
  750. y = CursorSel->y;
  751. w = CursorSel->w;
  752. h = CursorSel->h;
  753. XSetForeground(display,gc,Red.pixel);
  754. XDrawRectangle(display,Cow,gc,x,y,FixFontWidth,h);
  755. XDrawRectangle(display,Cow,gc,x-1,y-1,FixFontWidth+2,h+2);
  756. }
  757. }
  758. void RemoveBlanks(char *p)
  759. {
  760. int l;
  761. l = strlen(p);
  762. while (l && p[l-1] == ' ') p[--l] = 0;
  763. }
  764. struct SelStruct *NewSel(struct SelStruct *OldSel)
  765. {
  766. struct SelStruct *Sel;
  767. Sel = calloc(1,sizeof(struct SelStruct));
  768. OldSel->Next = Sel;
  769. Sel->Prev = OldSel;
  770. return Sel;
  771. }
  772. void NL_i(int m, char *list, char *name, int i)
  773. {
  774. if (NL_items > NL_MAX_ITEMS-2)
  775. {
  776. printf("\n*** Error ***\n");
  777. printf("Number of namelist items exceeds %d \n",NL_MAX_ITEMS);
  778. printf("Increase NL_MAX_ITEMS in most,c\n");
  779. exit(1);
  780. }
  781. NL_list[NL_items].model = m;
  782. strcpy(NL_list[NL_items].list,list);
  783. strcpy(NL_list[NL_items].name,name);
  784. NL_list[NL_items].flag = SEL_INT;
  785. NL_list[NL_items].ival = i;
  786. NL_list[NL_items].rval = 0.0;
  787. NL_list[NL_items].text[0] = 0;
  788. NL_items++;
  789. }
  790. void NL_r(int m, char *list, char *name, double r)
  791. {
  792. if (NL_items > NL_MAX_ITEMS-2)
  793. {
  794. printf("\n*** Error ***\n");
  795. printf("Number of namelist items exceeds %d \n",NL_MAX_ITEMS);
  796. printf("Increase NL_MAX_ITEMS in most,c\n");
  797. exit(1);
  798. }
  799. NL_list[NL_items].model = m;
  800. strcpy(NL_list[NL_items].list,list);
  801. strcpy(NL_list[NL_items].name,name);
  802. NL_list[NL_items].flag = SEL_REAL;
  803. NL_list[NL_items].ival = 0;
  804. NL_list[NL_items].rval = r;
  805. NL_list[NL_items].text[0] = 0;
  806. NL_items++;
  807. }
  808. void NL_p(char *name, float r[])
  809. {
  810. if (NL_items > NL_MAX_ITEMS-2)
  811. {
  812. printf("\n*** Error ***\n");
  813. printf("Number of namelist items exceeds %d \n",NL_MAX_ITEMS);
  814. printf("Increase NL_MAX_ITEMS in most,c\n");
  815. exit(1);
  816. }
  817. NL_list[NL_items].model = PLASIM;
  818. strcpy(NL_list[NL_items].list,"planet");
  819. strcpy(NL_list[NL_items].name,name);
  820. NL_list[NL_items].flag = SEL_PLANET;
  821. NL_list[NL_items].ival = 0;
  822. NL_list[NL_items].rval = 0.0;
  823. NL_list[NL_items].text[0] = 0;
  824. memcpy(NL_list[NL_items].pvec,r,sizeof(float) * PLANETS);
  825. NL_items++;
  826. }
  827. void NL_t(int m, char *list, char *name, char *t)
  828. {
  829. if (NL_items > NL_MAX_ITEMS-2)
  830. {
  831. printf("\n*** Error ***\n");
  832. printf("Number of namelist items exceeds %d \n",NL_MAX_ITEMS);
  833. printf("Increase NL_MAX_ITEMS in most,c\n");
  834. exit(1);
  835. }
  836. NL_list[NL_items].model = m;
  837. strcpy(NL_list[NL_items].list,list);
  838. strcpy(NL_list[NL_items].name,name);
  839. NL_list[NL_items].flag = SEL_TEVA;
  840. NL_list[NL_items].ival = 0;
  841. NL_list[NL_items].rval = 0.0;
  842. strcpy(NL_list[NL_items].text,t);
  843. NL_items++;
  844. }
  845. // Earth Mars Exo
  846. float eccen_vec[PLANETS] = { 0.016715, 0.09341233, 0.0};
  847. float mvelp_vec[PLANETS] = { 102.7 , 336.04084 , 0.0};
  848. float obliq_vec[PLANETS] = { 23.44 , 25.19 , 0.0};
  849. float gsol0_vec[PLANETS] = {1367.0 , 595.0 ,1000.0};
  850. void InitNamelist(void)
  851. {
  852. // Planet Simulator
  853. NL_p("ECCEN" , eccen_vec);
  854. NL_p("MVELP" , mvelp_vec);
  855. NL_p("OBLIQ" , obliq_vec);
  856. NL_p("GSOL0" , gsol0_vec);
  857. NL_i(PLASIM,"planet" ,"NFIXORB" , 0);
  858. NL_r(PLASIM,"radmod" ,"CO2" ,360.0);
  859. NL_i(PLASIM,"plasim" ,"KICK" , 1);
  860. NL_i(PLASIM,"plasim" ,"MPSTEP" , 0);
  861. //FL0318 NL_i(PLASIM,"plasim" ,"NAQUA" , 0);
  862. NL_i(PLASIM,"plasim" ,"NDIAG" , 0);
  863. //FL0318 NL_i(PLASIM,"plasim" ,"NGUIDBG" , 0);
  864. //FL0318 NL_i(PLASIM,"plasim" ,"NQSPEC" , 1);
  865. //FL0318 NL_i(PLASIM,"plasim" ,"NVEG" , 0);
  866. NL_i(PLASIM,"plasim" ,"NWPD" , 1);
  867. NL_i(PLASIM,"plasim" ,"NPRINT" , 0);
  868. //FL0318 NL_i(PLASIM,"plasim" ,"NSYNC" , 1);
  869. //FL0318 NL_i(PLASIM,"rainmod","NCLOUDS" , 1);
  870. //FL0318 NL_i(PLASIM,"rainmod","NSTORAIN", 0);
  871. //FL0318 NL_r(PLASIM,"plasim" ,"SYNCSTR", 0.0);
  872. if (Expert)
  873. {
  874. NL_i(PLASIM,"plasim" ,"NSYNC" , 0);
  875. NL_r(PLASIM,"plasim" ,"SYNCSTR", 0.0);
  876. }
  877. // SAM
  878. NL_i(SAM,"sam","KICK" , 1);
  879. NL_i(SAM,"sam","NAFTER" , 24);
  880. NL_i(SAM,"sam","NDEL" , 8);
  881. NL_i(SAM,"sam","NDIAG" ,240);
  882. NL_i(SAM,"sam","NEXP" , 4);
  883. NL_i(SAM,"sam","NGUIDBG", 0);
  884. NL_i(SAM,"sam","NTSPD" , 0);
  885. NL_r(SAM,"sam","DISP" ,0.0);
  886. NL_r(SAM,"sam","ROTSPD" ,1.0);
  887. // PUMA
  888. NL_i(PUMA,"puma","KICK" , 1);
  889. NL_i(PUMA,"puma","MPSTEP" , 0);
  890. NL_i(PUMA,"puma","NDEL" , 6);
  891. NL_i(PUMA,"puma","NDHEAT" , 0);
  892. NL_i(PUMA,"puma","NDIAG" , 0);
  893. NL_i(PUMA,"puma","NEWSR" , 0);
  894. NL_i(PUMA,"puma","NGUIDBG", 0);
  895. NL_i(PUMA,"puma","NHELSUA", 0);
  896. NL_i(PUMA,"puma","NSYNC" , 0);
  897. NL_i(PUMA,"puma","NWPD" , 1);
  898. NL_r(PUMA,"puma","DTEP" , 60);
  899. NL_r(PUMA,"puma","DTNS" ,-70.0);
  900. NL_r(PUMA,"puma","DTROP" , 12000.0);
  901. NL_r(PUMA,"puma","DTTRP" , 2.0);
  902. NL_r(PUMA,"puma","SYNCSTR", 0.0);
  903. NL_r(PUMA,"puma","ROTSPD" , 1.0);
  904. NL_r(PUMA,"puma","TGR" , 288.0);
  905. // CAT
  906. NL_r(CAT,"cat","ALPHA" , 0.0);
  907. NL_r(CAT,"cat","BETA" , 0.0);
  908. NL_r(CAT,"cat","AFORC" , 0.001);
  909. NL_r(CAT,"cat","TFORC" , 0.01);
  910. NL_i(CAT,"cat","NFORC" , 0);
  911. NL_i(CAT,"cat","NGUIDBG" , 0);
  912. NL_i(CAT,"cat","NPOST" , 0);
  913. NL_i(CAT,"cat","NSIM" , 0);
  914. NL_i(CAT,"cat","KFMIN" , 0);
  915. NL_i(CAT,"cat","KFMAX" , 8);
  916. NL_i(CAT,"cat","JACMTHD" , 1);
  917. NL_i(CAT,"cat","NSTEPS" , 10000);
  918. }
  919. void NamelistSelector(int model)
  920. {
  921. int i,yn,n,ml;
  922. struct SelStruct *Sel;
  923. int ledi_x,lbox_w,lbox_h;
  924. yn = opbox_y;
  925. n = 0;
  926. ml = 0;
  927. Sel = &SelModels[model];
  928. if (model == PLASIM)
  929. {
  930. Sel->type = SEL_TEXT;
  931. Sel->teco = HeadC;
  932. Sel->y = opbox_y;
  933. Sel->xt = nlpos_x;
  934. Sel->yt = Sel->y + BigFontAscent + 1;
  935. strcpy(Sel->text,PlanetName[Planet]);
  936. Sel->lt = strlen(Sel->text);
  937. yn += BigFontHeight + 1;
  938. Sel = NewSel(Sel);
  939. }
  940. for (i=0 ; i < NL_items ; ++i)
  941. if (NL_list[i].model == model)
  942. {
  943. Sel->Item = NL_list + i;
  944. strcpy(Sel->text,NL_list[i].name);
  945. Sel->lt = strlen(Sel->text);
  946. Sel->type = NL_list[i].flag;
  947. Sel->teco = BlackPix;
  948. Sel->h = FixFontHeight + 1;
  949. Sel->y = yn;
  950. Sel->xt = nlpos_x;
  951. Sel->yt = Sel->y + FixFont->ascent + 1;
  952. if (Sel->lt > ml) ml = Sel->lt;
  953. if (Sel->lt > ml) ml = Sel->lt;
  954. if (NL_list[i].flag == SEL_INT)
  955. {
  956. Sel->w = 6 * FixFontWidth + 2;
  957. Sel->edco = 6;
  958. Sel->div = Sel->iv = NL_list[i].ival;
  959. }
  960. else if (NL_list[i].flag == SEL_REAL)
  961. {
  962. Sel->edco = 11;
  963. Sel->w = 11 * FixFontWidth + 2;
  964. Sel->dfv = Sel->fv = NL_list[i].rval;
  965. }
  966. else if (NL_list[i].flag == SEL_PLANET)
  967. {
  968. Sel->edco = 11;
  969. Sel->w = 11 * FixFontWidth + 2;
  970. Sel->dfv = Sel->fv = NL_list[i].pvec[Planet];
  971. memcpy(Sel->fpl,NL_list[i].pvec,sizeof(float) * PLANETS);
  972. }
  973. else
  974. {
  975. Sel->edco = 11;
  976. Sel->w = 11 * FixFontWidth + 2;
  977. strncpy(Sel->teva,NL_list[i].text,15);
  978. }
  979. yn += FixFontHeight + 1;
  980. Sel = NewSel(Sel);
  981. ++n;
  982. }
  983. Sel->Prev->Next = NULL; // Mark end of chain
  984. /* Compute size of namelist box */
  985. ledi_x = nlpos_x + (ml + 1) * FixFontWidth;
  986. lbox_w = (ml + 14) * FixFontWidth;
  987. lbox_h = BigFontHeight + (n+2) * FixFontHeight;
  988. if (nledi_x < ledi_x) nledi_x = ledi_x;
  989. if (nlbox_w < lbox_w) nlbox_w = lbox_w;
  990. if (nlbox_h < lbox_h) nlbox_h = lbox_h;
  991. UpdateSelections(ComEnd->Next);
  992. }
  993. void LoadFonts(void)
  994. {
  995. if (ScreenH < 700)
  996. {
  997. strcpy(ModFontName,"5x8");
  998. strcpy(FixFontName,"6x10");
  999. strcpy(BigFontName,"7x13");
  1000. }
  1001. else if (ScreenH < 800)
  1002. {
  1003. strcpy(ModFontName,"6x10");
  1004. strcpy(FixFontName,"7x13");
  1005. strcpy(BigFontName,"9x15bold");
  1006. }
  1007. if ((ModFont = XLoadQueryFont(display,ModFontName)) == NULL)
  1008. {
  1009. printf("%s: Cannot open %s font\n",progname,ModFontName);
  1010. exit(-1);
  1011. }
  1012. if ((FixFont = XLoadQueryFont(display,FixFontName)) == NULL)
  1013. {
  1014. printf("%s: Cannot open %s font\n",progname,FixFontName);
  1015. exit(-1);
  1016. }
  1017. if ((BigFont = XLoadQueryFont(display,BigFontName)) == NULL)
  1018. {
  1019. printf("%s: Cannot open %s font\n",progname,BigFontName);
  1020. exit(-1);
  1021. }
  1022. SamFont = XLoadQueryFont(display,"rk24");
  1023. ModFontWidth = XTextWidth(ModFont,"X",1);
  1024. FixFontWidth = XTextWidth(FixFont,"X",1);
  1025. BigFontWidth = XTextWidth(BigFont,"X",1);
  1026. FixFontAscent = FixFont->ascent;
  1027. ModFontAscent = ModFont->ascent;
  1028. BigFontAscent = BigFont->ascent;
  1029. ModFontHeight = ModFont->ascent + ModFont->descent;
  1030. FixFontHeight = FixFont->ascent + FixFont->descent;
  1031. BigFontHeight = BigFont->ascent + BigFont->descent;
  1032. if (Debug)
  1033. {
  1034. printf("ModFont %2d x %2d %s\n",ModFontWidth,ModFontHeight,ModFontName);
  1035. printf("FixFont %2d x %2d %s\n",FixFontWidth,FixFontHeight,FixFontName);
  1036. printf("BigFont %2d x %2d %s\n",BigFontWidth,BigFontHeight,BigFontName);
  1037. }
  1038. }
  1039. void InitSelections(void)
  1040. {
  1041. int i,l,n,dyn,dys,mw,x,dx;
  1042. char text[80];
  1043. struct SelStruct *Sel;
  1044. struct tm *dati;
  1045. FILE *fp;
  1046. opbox_x = FixFontWidth / 2;
  1047. dyn = FixFontHeight + 1;
  1048. dys = (3 * dyn) / 2;
  1049. // Initialize Sel at anchor SelStart
  1050. Sel = &SelStart;
  1051. // Model
  1052. Sel->type = SEL_TEXT;
  1053. Sel->teco = HeadC;
  1054. Sel->y = opbox_y;
  1055. Sel->yt = Sel->y + BigFontAscent + 1;
  1056. strcpy(Sel->text,"Model");
  1057. Sel->lt = strlen(Sel->text);
  1058. // PUMA
  1059. Sel = NewSel(Sel);
  1060. InitNextSelection(Sel,BigFontHeight,FullModelName[PUMA]);
  1061. Sel->type = SEL_CHECK;
  1062. Sel->teco = BlackPix;
  1063. Sel->h = FixFontHeight + 1;
  1064. Sel->w = FixFontHeight + 1;
  1065. Sel->yt = Sel->y + FixFontAscent + 1;
  1066. Sel->div = Sel->iv = 1;
  1067. SelMod = Sel;
  1068. // SAM
  1069. Sel = NewSel(Sel);
  1070. InitNextSelection(Sel,dyn,"SAM");
  1071. // Hide SAM ?
  1072. if (!SamEnabled)
  1073. {
  1074. Sel->no = 1;
  1075. Sel->lt = 0;
  1076. }
  1077. // CAT
  1078. Sel = NewSel(Sel);
  1079. InitNextSelection(Sel,dyn,FullModelName[CAT]);
  1080. // Hide CAT until released
  1081. if (!CatEnabled)
  1082. {
  1083. Sel->no = 1;
  1084. Sel->lt = 0;
  1085. }
  1086. // Planet Simulator
  1087. Sel = NewSel(Sel);
  1088. InitNextSelection(Sel,dyn,FullModelName[PLASIM]);
  1089. // Earth
  1090. Sel = NewSel(Sel);
  1091. InitNextSelection(Sel,dyn,"Earth");
  1092. SelPlanet[EARTH] = Sel;
  1093. Sel->div = Sel->iv = 1;
  1094. Sel->no = 1;
  1095. Sel->xo = (5 * FixFontWidth) / 2;
  1096. // Mars
  1097. Sel = NewSel(Sel);
  1098. InitNextSelection(Sel,dyn,"Mars");
  1099. SelPlanet[MARS] = Sel;
  1100. Sel->no = 1;
  1101. // Exo
  1102. Sel = NewSel(Sel);
  1103. InitNextSelection(Sel,dyn,"Exo");
  1104. SelPlanet[EXO] = Sel;
  1105. Sel->no = 1;
  1106. // Modules
  1107. Sel = NewSel(Sel);
  1108. InitNextSelection(Sel,dys,"Modules");
  1109. Sel->type = SEL_TEXT;
  1110. Sel->teco = HeadC;
  1111. Sel->h = 0;
  1112. Sel->w = 0;
  1113. Sel->yt = Sel->y + BigFontAscent + 1;
  1114. Sel->xo = 0;
  1115. // Mixed Layer Ocean
  1116. Sel = NewSel(Sel);
  1117. InitNextSelection(Sel,BigFontHeight,"ML Ocean");
  1118. Sel->type = SEL_CHECK;
  1119. Sel->teco = BlackPix;
  1120. Sel->h = FixFontHeight + 1;
  1121. Sel->w = FixFontHeight + 1;
  1122. Sel->yt = Sel->y + FixFontAscent + 1;
  1123. Sel->div = Sel->iv = 0;
  1124. Sel->no = 1;
  1125. SelOce = Sel;
  1126. Sel->piv = &Oce;
  1127. // LSG Ocean
  1128. if (LsgEnabled)
  1129. {
  1130. Sel = NewSel(Sel);
  1131. InitNextSelection(Sel,FixFontHeight,"LSG Ocean");
  1132. Sel->yt = Sel->y + FixFontAscent + 1;
  1133. Sel->div = Sel->iv = 0;
  1134. Sel->no = 1;
  1135. SelLsg = Sel;
  1136. Sel->piv = &Lsg;
  1137. }
  1138. // Ice
  1139. Sel = NewSel(Sel);
  1140. InitNextSelection(Sel,FixFontHeight,"Sea Ice");
  1141. Sel->type = SEL_CHECK;
  1142. Sel->teco = BlackPix;
  1143. Sel->h = FixFontHeight + 1;
  1144. Sel->w = FixFontHeight + 1;
  1145. Sel->yt = Sel->y + FixFontAscent + 1;
  1146. Sel->div = Sel->iv = 0;
  1147. Sel->no = 1;
  1148. SelIce = Sel;
  1149. Sel->piv = &Ice;
  1150. // Hardware
  1151. Sel = NewSel(Sel);
  1152. InitNextSelection(Sel,dys,"Parallelism");
  1153. Sel->type = SEL_TEXT;
  1154. Sel->teco = HeadC;
  1155. Sel->h = 0;
  1156. Sel->w = 0;
  1157. Sel->yt = Sel->y + BigFontAscent + 1;
  1158. // Number of CPUs
  1159. Sel = NewSel(Sel);
  1160. InitNextSelection(Sel,dys,"Cores");
  1161. Sel->h = FixFontHeight + 1;
  1162. Sel->w = FixFontHeight + 1;
  1163. Sel->yt = Sel->y + FixFont->ascent + 1;
  1164. Sel->w = 4 * FixFontWidth + 2;
  1165. Sel->type = SEL_INT;
  1166. Sel->edco = 4;
  1167. Sel->div = Sel->iv = Cores;
  1168. Sel->piv = &Cores;
  1169. SelCPU = Sel;
  1170. fp = fopen("most_compiler_mpi","r");
  1171. if (fp) fclose(fp);
  1172. else Sel->no = 1;
  1173. // Number of synchronous runs
  1174. if (MultirunEnabled && Expert)
  1175. {
  1176. Sel = NewSel(Sel);
  1177. InitNextSelection(Sel,dys,"Instances");
  1178. Sel->h = FixFontHeight + 1;
  1179. Sel->w = FixFontHeight + 1;
  1180. Sel->yt = Sel->y + FixFont->ascent + 1;
  1181. Sel->w = 4 * FixFontWidth + 2;
  1182. Sel->type = SEL_INT;
  1183. Sel->edco = 4;
  1184. Sel->div = Sel->iv = Multirun;
  1185. Sel->piv = &Multirun;
  1186. SelMulti = Sel;
  1187. fp = fopen("most_compiler_mpi","r");
  1188. if (fp) fclose(fp);
  1189. else Sel->no = 1;
  1190. }
  1191. // Resolution
  1192. Sel = NewSel(Sel);
  1193. InitNextSelection(Sel,dys,"Resolution");
  1194. Sel->type = SEL_TEXT;
  1195. Sel->h = 0;
  1196. Sel->w = 0;
  1197. Sel->yt = Sel->y + BigFont->ascent + 1;
  1198. // Horizontal resolution
  1199. if (Expert)
  1200. {
  1201. Sel = NewSel(Sel);
  1202. InitNextSelection(Sel,dys,"Latitudes #1");
  1203. Sel->h = FixFontHeight + 1;
  1204. Sel->yt = Sel->y + FixFont->ascent + 1;
  1205. Sel->w = 4 * FixFontWidth + 2;
  1206. Sel->edco = 4;
  1207. Sel->type = SEL_INT;
  1208. Sel->div = Sel->iv = Latitudes;
  1209. Sel->piv = &Latitudes;
  1210. DimText1 = Sel->text;
  1211. Sel = NewSel(Sel);
  1212. InitNextSelection(Sel,dys,"Latitudes #2");
  1213. Sel->div = Sel->iv = Latitudes;
  1214. Sel->piv = &Latitude2;
  1215. SelLat2 = Sel;
  1216. DimText2 = Sel->text;
  1217. }
  1218. else
  1219. {
  1220. Sel = NewSel(Sel);
  1221. InitNextSelection(Sel,dys,"T21 [64x32]");
  1222. Sel->type = SEL_CHECK;
  1223. Sel->h = FixFontHeight + 1;
  1224. Sel->w = FixFontHeight + 1;
  1225. Sel->yt = Sel->y + FixFont->ascent + 1;
  1226. Sel->div = Sel->iv = 1;
  1227. SelRes = Sel;
  1228. DimText1 = Sel->text;
  1229. Sel = NewSel(Sel);
  1230. InitNextSelection(Sel,dyn,"T31 [96x48]");
  1231. Sel->type = SEL_CHECK;
  1232. Sel->h = FixFontHeight + 1;
  1233. Sel->w = FixFontHeight + 1;
  1234. Sel->div = Sel->iv = 0;
  1235. DimText2 = Sel->text;
  1236. Sel = NewSel(Sel);
  1237. InitNextSelection(Sel,dyn,"T42 [128x64]");
  1238. Sel->type = SEL_CHECK;
  1239. Sel->h = FixFontHeight + 1;
  1240. Sel->w = FixFontHeight + 1;
  1241. Sel->div = Sel->iv = 0;
  1242. DimText3 = Sel->text;
  1243. }
  1244. // Vertical resolution
  1245. if (Expert)
  1246. {
  1247. Sel = NewSel(Sel);
  1248. InitNextSelection(Sel,dys,"Levels");
  1249. Sel->type = SEL_INT;
  1250. Sel->div = Sel->iv = Levels;
  1251. Sel->piv = &Levels;
  1252. }
  1253. // Options
  1254. Sel = NewSel(Sel);
  1255. InitNextSelection(Sel,dys,"Options");
  1256. Sel->type = SEL_TEXT;
  1257. Sel->h = 0;
  1258. Sel->w = 0;
  1259. Sel->yt = Sel->y + BigFont->ascent + 1;
  1260. // Global debug switch
  1261. Sel = NewSel(Sel);
  1262. InitNextSelection(Sel,dys,"Debug mode");
  1263. Sel->type = SEL_CHECK;
  1264. Sel->h = FixFontHeight + 1;
  1265. Sel->w = FixFontHeight + 1;
  1266. Sel->yt = Sel->y + FixFont->ascent + 1;
  1267. Sel->div = Sel->iv = 0;
  1268. Sel->piv = &ndebug;
  1269. // Precision switch
  1270. Sel = NewSel(Sel);
  1271. InitNextSelection(Sel,dyn,"Double Precision");
  1272. Sel->piv = &nprec;
  1273. // Global output switch
  1274. Sel = NewSel(Sel);
  1275. InitNextSelection(Sel,dys,"Write Output");
  1276. Sel->type = SEL_CHECK;
  1277. Sel->h = FixFontHeight + 1;
  1278. Sel->w = FixFontHeight + 1;
  1279. Sel->yt = Sel->y + FixFont->ascent + 1;
  1280. Sel->div = Sel->iv = 0;
  1281. Sel->piv = &noutput;
  1282. // GUI
  1283. Sel = NewSel(Sel);
  1284. InitNextSelection(Sel,dyn,"Run with GUI");
  1285. Sel->type = SEL_CHECK;
  1286. Sel->h = FixFontHeight + 1;
  1287. Sel->w = FixFontHeight + 1;
  1288. Sel->div = Sel->iv = 1;
  1289. Sel->piv = &ngui;
  1290. // Orography
  1291. Sel = NewSel(Sel);
  1292. InitNextSelection(Sel,dyn,"Orography");
  1293. Sel->type = SEL_CHECK;
  1294. //FL0318 Sel->div = Sel->iv = 1;
  1295. Sel->div = Sel->iv = 0;
  1296. Sel->piv = &noro;
  1297. SelOro = Sel;
  1298. // Annual cycle
  1299. Sel = NewSel(Sel);
  1300. InitNextSelection(Sel,dyn,"Annual cycle");
  1301. Sel->type = SEL_CHECK;
  1302. //FL0318 Sel->div = Sel->iv = 1;
  1303. Sel->div = Sel->iv = 0;
  1304. SelAnn = Sel;
  1305. // Experiment
  1306. Sel = NewSel(Sel);
  1307. InitNextSelection(Sel,dys,"Simulation");
  1308. Sel->type = SEL_TEXT;
  1309. Sel->h = 0;
  1310. Sel->w = 0;
  1311. Sel->yt = Sel->y + BigFont->ascent + 1;
  1312. // Simulation Start
  1313. dati = localtime(&CurrentDate);
  1314. Sel = NewSel(Sel);
  1315. InitNextSelection(Sel,dys,"Start year");
  1316. Sel->h = FixFontHeight + 1;
  1317. Sel->yt = Sel->y + FixFont->ascent + 1;
  1318. Sel->w = 4 * FixFontWidth + 2;
  1319. Sel->edco = 4;
  1320. Sel->type = SEL_INT;
  1321. Sel->div = Sel->iv = 1;
  1322. Sel->piv = &SimStart;
  1323. SelSYear = Sel;
  1324. // Years
  1325. Sel = NewSel(Sel);
  1326. InitNextSelection(Sel,dyn,"Years to run");
  1327. Sel->h = FixFontHeight + 1;
  1328. Sel->yt = Sel->y + FixFont->ascent + 1;
  1329. Sel->w = 4 * FixFontWidth + 2;
  1330. Sel->type = SEL_INT;
  1331. Sel->div = Sel->iv = 10;
  1332. Sel->piv = &SimYears;
  1333. opbox_h = Sel->y + dys - opbox_y;
  1334. // Orography edit
  1335. Sel = NewSel(Sel);
  1336. InitNextSelection(Sel,dys,"Change [gpm]");
  1337. Sel->h = FixFontHeight + 1;
  1338. Sel->yt = Sel->y + FixFont->ascent + 1;
  1339. Sel->w = 6 * FixFontWidth + 2;
  1340. Sel->type = SEL_INT;
  1341. Sel->div = Sel->iv = 0;
  1342. Sel->piv = &OroAno;
  1343. SelAno = Sel;
  1344. // Ground temperature edit
  1345. /*
  1346. Sel = NewSel(Sel);
  1347. InitNextSelection(Sel,dys,"Change [K]");
  1348. Sel->h = FixFontHeight + 1;
  1349. Sel->yt = Sel->y + FixFont->ascent + 1;
  1350. Sel->w = 6 * FixFontWidth + 2;
  1351. Sel->type = SEL_INT;
  1352. Sel->div = Sel->iv = 0;
  1353. Sel->piv = &TgrAno;
  1354. SelTgr = Sel;
  1355. */
  1356. // Mark end of common selection chain
  1357. ComEnd = Sel;
  1358. mw = 0;
  1359. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1360. if (Sel->w > mw) mw = Sel->w;
  1361. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1362. {
  1363. Sel->xt = Sel->xo + FixFontWidth * 2 + mw;
  1364. if (!Sel->x) Sel->x = Sel->xt - Sel->w - FixFontWidth;
  1365. if (Sel->type == SEL_INT )
  1366. {
  1367. sprintf(text,"%10d",Sel->iv);
  1368. strcpy(Sel->teva,text+10-Sel->edco);
  1369. }
  1370. }
  1371. for (x = 0 , Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1372. {
  1373. if (Sel->type == SEL_TEXT) l = Sel->lt * BigFontWidth;
  1374. else l = Sel->lt * FixFontWidth;
  1375. if (x < l) x = l;
  1376. }
  1377. opbox_w = x + mw + 2 * FixFontWidth;
  1378. nlbox_x = opbox_x + opbox_w + FixFontWidth;
  1379. nlbox_y = opbox_y;
  1380. nlpos_x = nlbox_x + FixFontWidth;
  1381. }
  1382. void CheckMark(int x, int y, int d)
  1383. {
  1384. XSetForeground(display,gc,LightGreen.pixel);
  1385. XFillArc(display,Cow,gc,x+1,y+1,d-5,d-5,0,FULLARC);
  1386. }
  1387. void DisplayTeVa(int x, int y, int w, char *t)
  1388. {
  1389. int l;
  1390. l = strlen(t);
  1391. if (l > w / FixFontWidth) l = w / FixFontWidth;
  1392. XSetForeground(display,gc,WhitePix);
  1393. XSetBackground(display,gc,BlackPix);
  1394. if (l) XDrawImageString(display,Cow,gc,x,y,t,l);
  1395. }
  1396. void ShowSelection(struct SelStruct *Sel)
  1397. {
  1398. int x,y,h,w;
  1399. if (Sel->hide) return;
  1400. h = Sel->h;
  1401. w = Sel->w;
  1402. x = Sel->x;
  1403. y = Sel->y;
  1404. if (!Sel->no)
  1405. {
  1406. if (Sel->type == SEL_CHECK)
  1407. {
  1408. XSetForeground(display,gc,TextC);
  1409. XDrawArc(display,Cow,gc,x,y,w-2,h-2,0,FULLARC);
  1410. if (Sel->iv) CheckMark(x,y,w+1);
  1411. }
  1412. else DisplayTeVa(x+1,Sel->yt,w,Sel->teva);
  1413. }
  1414. XSetForeground(display,gc,TextC);
  1415. XSetBackground(display,gc,WinBG);
  1416. if (Sel->type == SEL_TEXT)
  1417. {
  1418. XSetFont(display, gc, BigFont->fid);
  1419. XSetForeground(display,gc,Sel->teco);
  1420. XDrawImageString(display,Cow,gc,Sel->xt,Sel->yt,Sel->text,Sel->lt);
  1421. XSetFont(display, gc, FixFont->fid);
  1422. }
  1423. else
  1424. XDrawImageString(display,Cow,gc,Sel->xt,Sel->yt,Sel->text,Sel->lt);
  1425. }
  1426. void InitLogo(void)
  1427. {
  1428. int n;
  1429. // KlimaCampus
  1430. n = 0;
  1431. Logo[n].x = FixFontWidth;
  1432. Logo[n].y = 8;
  1433. // PUMA
  1434. ++n;
  1435. strcpy(Logo[n].t[0],FullModelName[PUMA]);
  1436. // Plasim
  1437. ++n;
  1438. strcpy(Logo[n].t[0],FullModelName[PLASIM]);
  1439. // Cat
  1440. ++n;
  1441. strcpy(Logo[n].t[0],FullModelName[CAT]);
  1442. Logos = n+1;
  1443. }
  1444. void GenerateNames(void)
  1445. {
  1446. Truncation = (2 * Latitudes - 1) / 3;
  1447. sprintf(namelist_name,"%s_namelist",ShortModelName[Model]);
  1448. if (Model == CAT)
  1449. {
  1450. if (Cores < 2) strcpy(exec_name,"most_cat.x");
  1451. else strcpy(exec_name,"most_cat_mpi.x");
  1452. }
  1453. else if (Model == PUMA)
  1454. {
  1455. if (Cores < 2) strcpy(exec_name,"most_puma.x");
  1456. else strcpy(exec_name,"most_puma_mpi.x");
  1457. }
  1458. else if (Model == SAM)
  1459. {
  1460. if (Cores < 2) strcpy(exec_name,"most_sam.x");
  1461. else strcpy(exec_name,"most_sam_mpi.x");
  1462. }
  1463. else
  1464. sprintf(exec_name,"most_%s_t%d_l%d_p%d.x",
  1465. ShortModelName[Model],Truncation,Levels,Cores);
  1466. sprintf(diag_name,"%s_diag",ShortModelName[Model]);
  1467. sprintf(outp_name,"%s_output",ShortModelName[Model]);
  1468. sprintf(build_name,"most_%s_build",ShortModelName[Model]);
  1469. sprintf(build_ppp,"most_%s_build","ppp");
  1470. sprintf(run_name,"most_%s_run",ShortModelName[Model]);
  1471. if (Latitudes < 1000) sprintf(oro_name,"N%3.3d_surf_0129.sra",Latitudes);
  1472. else sprintf(oro_name,"N%d_surf_0129.sra",Latitudes);
  1473. }
  1474. int WriteRunScript(int model)
  1475. {
  1476. int i;
  1477. int porm;
  1478. FILE *fp;
  1479. char command[256];
  1480. char run[256];
  1481. strcpy(exec_nam2,exec_name); // Duplicate exec name
  1482. if (model == CAT) // Add Dimensions
  1483. {
  1484. sprintf(exec_name+strlen(exec_name)," %d %d",Latitudes,Latitude2);
  1485. }
  1486. if (model == PUMA) // Add Latitudes and Levels as arguments
  1487. {
  1488. sprintf(exec_name+strlen(exec_name)," %d %d",Latitudes,Levels);
  1489. sprintf(exec_nam2+strlen(exec_nam2)," %d %d",Latitude2,Levels);
  1490. }
  1491. if (model == SAM) // Add Latitudes as arguments
  1492. {
  1493. sprintf(exec_name+strlen(exec_name)," %d",Latitudes);
  1494. sprintf(exec_nam2+strlen(exec_nam2)," %d",Latitude2);
  1495. }
  1496. // porm is for Parallel OR Multiprocessing
  1497. porm = MAX(Cores,Multirun);
  1498. strcpy(run,ShortModelName[model]);
  1499. strcat(run,"/run/");
  1500. strcat(run,run_name);
  1501. fp = fopen(run,"w");
  1502. if (fp == NULL)
  1503. {
  1504. printf("Could not open file <%s> for writing\n",run);
  1505. return 0; /* Failure */
  1506. }
  1507. fputs("#!/bin/bash\n",fp);
  1508. fprintf(fp,"# run-script generated by Most %s",ctime(&CurrentDate));
  1509. fputs("EXP=MOST # Name your experiment here\n",fp);
  1510. fprintf(fp,"[ $# == 1 ] && cd $1\n");
  1511. fprintf(fp,"rm -f %s_restart\n",ShortModelName[model]);
  1512. fputs("rm -f Abort_Message\n",fp);
  1513. fputs("YEAR=0\n",fp);
  1514. fprintf(fp,"YEARS=%d\n",SimYears);
  1515. fputs("srv2nc=../../tools/srv2nc\n",fp);
  1516. if (Multirun > 1) fprintf(fp,"INSTANCES=%d\n",Multirun);
  1517. if (ngui) fputs("# Remove '#' from 'while' and 'end' lines for restart loop\n",fp);
  1518. if (ngui) fputs("# ",fp); /* deactivate loop for GUI case */
  1519. fputs("while [ $YEAR -lt $YEARS ]\n",fp);
  1520. if (ngui) fputs("# ",fp); /* deactivate loop for GUI case */
  1521. fputs("do\n",fp);
  1522. fputs(" YEAR=`expr $YEAR + 1`\n",fp);
  1523. fputs(" DATANAME=`printf '%s.%03d' $EXP $YEAR`\n",fp);
  1524. fputs(" DIAGNAME=`printf '%s_DIAG.%03d' $EXP $YEAR`\n",fp);
  1525. fputs(" RESTNAME=`printf '%s_REST.%03d' $EXP $YEAR`\n",fp);
  1526. fputs(" OCENAME=`printf '%s_OCE.%03d.nc' $EXP $YEAR`\n",fp);
  1527. fputs(" ICENAME=`printf '%s_ICE.%03d.nc' $EXP $YEAR`\n",fp);
  1528. if (porm < 2)
  1529. {
  1530. fprintf(fp," ./%s\n",exec_name);
  1531. }
  1532. else
  1533. {
  1534. if (Multirun > 1)
  1535. {
  1536. fprintf(fp," %s",mpirun);
  1537. fprintf(fp," -np 1 %s : -np 1 %s\n",exec_name,exec_nam2);
  1538. }
  1539. else
  1540. {
  1541. fprintf(fp," %s -np %d %s\n",mpirun,porm,exec_name);
  1542. }
  1543. }
  1544. fputs(" [ -e Abort_Message ] && exit 1\n",fp);
  1545. if (Multirun > 1)
  1546. {
  1547. fputs(" INST=0\n",fp);
  1548. fputs(" while [ $INST -lt $INSTANCES ]\n do\n",fp);
  1549. fputs(" EXT=`printf '%02d' $INST`\n",fp);
  1550. fprintf(fp," [ -e %s_$EXT ] && mv %s_$EXT ${EXT}_$DATANAME\n",
  1551. outp_name,outp_name);
  1552. fprintf(fp," mv %s_$EXT ${EXT}_$DIAGNAME\n",diag_name);
  1553. fprintf(fp," cp %s_status_$EXT %s_restart_$EXT\n",
  1554. ShortModelName[model],ShortModelName[model]);
  1555. fprintf(fp," mv %s_status_$EXT ${EXT}_$RESTNAME\n",ShortModelName[model]);
  1556. fputs(" INST=`expr $INST + 1`\n",fp);
  1557. fputs(" done\n",fp);
  1558. }
  1559. else
  1560. {
  1561. fprintf(fp," [ -e %s ] && mv %s $DATANAME\n",outp_name,outp_name);
  1562. fprintf(fp," [ -e %s ] && mv %s $DIAGNAME\n",diag_name,diag_name);
  1563. fprintf(fp," [ -e ocean_output ] && $srv2nc ocean_output $OCENAME\n");
  1564. fprintf(fp," [ -e ice_output ] && $srv2nc ice_output $ICENAME\n");
  1565. fprintf(fp," [ -e %s_status ] && cp %s_status %s_restart\n",
  1566. ShortModelName[model],ShortModelName[model],ShortModelName[model]);
  1567. fprintf(fp," [ -e %s_status ] && mv %s_status $RESTNAME\n",
  1568. ShortModelName[model],ShortModelName[model]);
  1569. }
  1570. if (ngui) fputs("# ",fp); /* deactivate loop for GUI case */
  1571. fputs("done\n",fp);
  1572. fclose(fp);
  1573. sprintf(command,"chmod a+x %s",run);
  1574. system(command);
  1575. return 1; /* Success */
  1576. }
  1577. void BlueMessage(char *mess)
  1578. {
  1579. int l,x,y,w,h;
  1580. XSetFont(display, gc, FixFont->fid);
  1581. l = strlen(mess);
  1582. w = FixFontWidth * (l + 4);
  1583. h = FixFontHeight * 3;
  1584. if (w > WinXSize) w = WinXSize;
  1585. XSetForeground(display,gc,Blue.pixel);
  1586. x = (WinXSize - w) / 2;
  1587. y = (WinYSize - h) / 2;
  1588. XFillRectangle(display,Cow,gc,x,y,w,h);
  1589. XSetForeground(display,gc,WhitePix);
  1590. XSetBackground(display,gc,Blue.pixel);
  1591. XDrawRectangle(display,Cow,gc,x-1,y-1,w+1,h+1);
  1592. x += 2 * FixFontWidth;
  1593. y += 2 * FixFontHeight;
  1594. XDrawImageString(display,Cow,gc,x,y,mess,l);
  1595. XSync(display,0);
  1596. }
  1597. void RedMessage(char *mess)
  1598. {
  1599. int l,x,y,w,h;
  1600. XSetFont(display, gc, FixFont->fid);
  1601. l = strlen(mess);
  1602. w = FixFontWidth * (l + 4);
  1603. h = FixFontHeight * 3;
  1604. if (w > WinXSize) w = WinXSize;
  1605. XSetForeground(display,gc,Red.pixel);
  1606. x = (WinXSize - w) / 2;
  1607. y = (WinYSize - h) / 2;
  1608. XFillRectangle(display,Cow,gc,x,y,w,h);
  1609. XSetForeground(display,gc,WhitePix);
  1610. XSetBackground(display,gc,Red.pixel);
  1611. XDrawRectangle(display,Cow,gc,x-1,y-1,w+1,h+1);
  1612. x += 2 * FixFontWidth;
  1613. y += 2 * FixFontHeight;
  1614. XDrawImageString(display,Cow,gc,x,y,mess,l);
  1615. XSync(display,0);
  1616. }
  1617. int WriteRunPPP(void)
  1618. {
  1619. int i;
  1620. FILE *fp;
  1621. char command[256];
  1622. char run[256];
  1623. strcpy(run,ShortModelName[Model]);
  1624. strcat(run,"/run/");
  1625. strcat(run,run_ppp);
  1626. fp = fopen(run,"w");
  1627. if (fp == NULL)
  1628. {
  1629. printf("Could not open file <%s> for writing\n",run);
  1630. return 0; /* Failure */
  1631. }
  1632. fputs("#!/bin/bash\n",fp);
  1633. fprintf(fp,"# run-script generated by Most %s",ctime(&CurrentDate));
  1634. fputs("[ $# == 1 ] && cd $1\n",fp);
  1635. if (OroClear) fprintf(fp,"rm -f %s\n",oro_name);
  1636. else if (!OroAqua)
  1637. fprintf(fp,"[ ! -e %s -a -e ../dat/%s ] && cp -p ../dat/%s .\n",
  1638. oro_name,oro_name,oro_name);
  1639. fprintf(fp," ./%s >!ppp.out\n",exec_ppp);
  1640. fclose(fp);
  1641. sprintf(command,"chmod a+x %s",run);
  1642. system(command);
  1643. strcpy(command,run);
  1644. BlueMessage("Running PUMA Preprocessor");
  1645. sprintf(command,"%s/run/%s %s/run",ShortModelName[Model],run_ppp,ShortModelName[Model]);
  1646. system(command);
  1647. OroAno = 0; /* Anomaly applied -> reset it */
  1648. SelAno->iv = 0;
  1649. return 1; /* Success */
  1650. }
  1651. void WriteResmod(char *res)
  1652. {
  1653. FILE *fp;
  1654. int OldLat,OldLev,OldPro;
  1655. char Line[80];
  1656. // Read existing file if there and check for changes
  1657. fp = fopen(res,"r");
  1658. if (fp)
  1659. {
  1660. fgets(Line,80,fp); // header line 1
  1661. fgets(Line,80,fp); // header line 2
  1662. fgets(Line,80,fp);
  1663. OldLat = atoi(Line+27);
  1664. fgets(Line,80,fp);
  1665. OldLev = atoi(Line+27);
  1666. fgets(Line,80,fp);
  1667. OldPro = atoi(Line+27);
  1668. fclose(fp);
  1669. if (OldLat == Latitudes && OldLev == Levels && OldPro == Cores) return;
  1670. }
  1671. fp = fopen(res,"w");
  1672. if (fp == NULL)
  1673. {
  1674. printf("Could not open file <%s> for writing\n",res);
  1675. exit(1); /* Failure */
  1676. }
  1677. fprintf(fp," module resmod ! generated by MoSt %s\n",ctime(&CurrentDate));
  1678. fprintf(fp," parameter(NLAT_ATM = %d)\n",Latitudes);
  1679. fprintf(fp," parameter(NLEV_ATM = %d)\n",Levels);
  1680. fprintf(fp," parameter(NPRO_ATM = %d)\n",Cores);
  1681. fprintf(fp," end module resmod\n");
  1682. fclose(fp);
  1683. }
  1684. int Build(int model)
  1685. {
  1686. int i,l,x,y,ierr;
  1687. int porm;
  1688. FILE *fp;
  1689. char script_backup[256];
  1690. char command[256];
  1691. char message[256];
  1692. char bld[256];
  1693. char res[256];
  1694. char *shomo;
  1695. porm = MAX(Cores,Multirun);
  1696. shomo = ShortModelName[model];
  1697. strcpy(bld,shomo);
  1698. strcat(bld,"/bld/");
  1699. strcpy(res,bld);
  1700. strcat(bld,build_name);
  1701. strcat(res,res_name);
  1702. strcpy(script_backup,bld);
  1703. strcat(script_backup,".bak");
  1704. rename(bld,script_backup);
  1705. if (model == PLASIM) WriteResmod(res);
  1706. fp = fopen(bld,"w");
  1707. if (fp == NULL)
  1708. {
  1709. printf("Could not open file <%s> for writing\n",bld);
  1710. return 0; /* Failure */
  1711. }
  1712. fputs("#!/bin/bash\n",fp);
  1713. fprintf(fp,"# compile-script generated by Most %s",ctime(&CurrentDate));
  1714. fputs("[ $# == 1 ] && cd $1\n",fp);
  1715. if (ForceRebuild) fputs("rm -f *.x *.o *.mod\n",fp);
  1716. fputs("cp -p ../src/* .\n",fp);
  1717. if (ngui == 0) // Use stub routines and switch off X11 lib
  1718. {
  1719. putenv("GUIMOD=guimod_stub");
  1720. putenv("PUMAX=pumax_stub");
  1721. putenv("GUIX11=guix11_stub");
  1722. putenv("GUILIB=");
  1723. }
  1724. if (Planet == MARS) putenv("PLAMOD=p_mars");
  1725. if (Planet == EXO ) putenv("PLAMOD=p_exo");
  1726. if (Lsg)
  1727. {
  1728. fputs("cp -p ../../lsg/src/lsgmod.f90 .\n",fp);
  1729. fputs("cat lsgmod.f90\n",fp);
  1730. putenv("OCEANCOUP=cpl");
  1731. }
  1732. else
  1733. {
  1734. putenv("OCEANCOUP=cpl_stub");
  1735. }
  1736. if (Latitudes < 4) putenv("FFTMOD=fft991mod");
  1737. if (porm > 1)
  1738. {
  1739. fputs("[ ! -e MPI ] && rm -f *.o *.mod *.x\n",fp);
  1740. fputs("touch MPI\n",fp);
  1741. fputs("cat ../../most_compiler_mpi",fp);
  1742. if (Multirun > 1) putenv("MPIMOD=mpimod_multi");
  1743. }
  1744. else
  1745. {
  1746. fputs("[ -e MPI ] && rm -f *.o *.mod *.x MPI\n",fp);
  1747. fputs("cat ../../most_compiler",fp);
  1748. }
  1749. if (ndebug) fputs(" ../../most_debug_options",fp);
  1750. if (nprec)
  1751. {
  1752. fputs(" ../../most_precision_options",fp);
  1753. }
  1754. fprintf(fp," make_%s > makefile\n",shomo);
  1755. fputs("source ../../modules.load\n",fp);
  1756. fputs("make\n",fp);
  1757. fprintf(fp,"[ $? == 0 ] && cp %s.x ../bin/%s\n",shomo,exec_name);
  1758. fclose(fp);
  1759. sprintf(command,"chmod a+x %s",bld);
  1760. system(command);
  1761. sprintf(message,"Building %s - wait a minute!",FullModelName[Model]);
  1762. BlueMessage(message);
  1763. strcat(bld," ");
  1764. strcat(bld,shomo);
  1765. strcat(bld,"/bld");
  1766. if (system(bld))
  1767. {
  1768. RedMessage("Error in build process");
  1769. sleep(5);
  1770. return 0; // error
  1771. }
  1772. sprintf(command,"cp %s/bin/%s %s/run\n",shomo,exec_name,shomo);
  1773. system(command);
  1774. // copy CAT simulation namelist to run directory
  1775. if (model == CAT)
  1776. {
  1777. sprintf(command,"cp cat/dat/sim_%4.4d.nl cat/run/sim_namelist\n",CatSim);
  1778. system(command);
  1779. }
  1780. // copy hires background bitmap for Earth or Mars
  1781. if (model == PUMA || model == PLASIM )
  1782. {
  1783. if (model == PLASIM && Planet == MARS)
  1784. {
  1785. sprintf(command,"cp images/mars.bmp %s/run/map.bmp\n",shomo);
  1786. system(command);
  1787. }
  1788. else if (model == PLASIM && Planet == EARTH)
  1789. {
  1790. sprintf(command,"cp images/earth.bmp %s/run/map.bmp\n",shomo);
  1791. system(command);
  1792. }
  1793. }
  1794. // copy surface data in matching resolution to run directory
  1795. if (model == PLASIM)
  1796. {
  1797. if (Planet == MARS)
  1798. sprintf(command,"cp plasim/dat/T%d_mars/* plasim/run/\n",Truncation);
  1799. else if (Planet == EXO)
  1800. sprintf(command,"cp plasim/dat/T%d_exo/* plasim/run/\n",Truncation);
  1801. else
  1802. sprintf(command,"cp plasim/dat/T%d/* plasim/run/\n",Truncation);
  1803. system(command);
  1804. if (Lsg) // copy data for LSG ocean model and select LSG GUI configuration
  1805. {
  1806. sprintf(command,"cp lsg/dat/* %s/run\n",shomo);
  1807. system(command);
  1808. sprintf(command,"cp %s/dat/GUI_LSG.cfg %s/run/GUI.cfg\n",shomo,shomo);
  1809. system(command);
  1810. }
  1811. else
  1812. {
  1813. sprintf(command,"cp %s/dat/GUI.cfg %s/run\n",shomo,shomo);
  1814. system(command);
  1815. }
  1816. }
  1817. else
  1818. {
  1819. sprintf(command,"cp %s/dat/GUI.cfg %s/run\n",shomo,shomo);
  1820. system(command);
  1821. }
  1822. // copy second GUI configuration for two-instances run
  1823. if (Multirun > 1)
  1824. {
  1825. sprintf(command,"cp %s/dat/GUI_0?.cfg %s/run\n",shomo,shomo);
  1826. system(command);
  1827. }
  1828. return 1; /* Success */
  1829. }
  1830. int BuildPPP(void)
  1831. {
  1832. int i,l,x,y;
  1833. FILE *fp;
  1834. char script_backup[256];
  1835. char command[256];
  1836. char message[256];
  1837. char bld[256];
  1838. char res[256];
  1839. strcpy(bld,ShortModelName[Model]);
  1840. strcat(bld,"/bld/");
  1841. strcpy(res,bld);
  1842. strcat(bld,build_ppp);
  1843. strcat(res,res_name);
  1844. strcpy(script_backup,bld);
  1845. strcat(script_backup,".bak");
  1846. rename(bld,script_backup);
  1847. fp = fopen(bld,"w");
  1848. if (fp == NULL)
  1849. {
  1850. printf("Could not open file <%s> for writing\n",bld);
  1851. return 0; /* Failure */
  1852. }
  1853. fputs("#!/bin/bash\n",fp);
  1854. fprintf(fp,"# compile-script generated by Most %s",ctime(&CurrentDate));
  1855. fputs("[ $# == 1 ] && cd $1\n",fp);
  1856. fputs("rm -f *.o *.mod\n",fp);
  1857. fputs("cp ../src/make_ppp .\n",fp);
  1858. fputs("cp ../src/*.f90 .\n",fp);
  1859. fputs("cat ../../most_compiler",fp);
  1860. if (ndebug) fputs(" ../../most_debug_options",fp);
  1861. if (nprec) fputs(" ../../most_precision_options",fp);
  1862. fprintf(fp," make_ppp > makefile_ppp\n");
  1863. fputs("make -e -f makefile_ppp\n",fp);
  1864. fprintf(fp,"cp ppp.x ../bin/%s\n",exec_ppp);
  1865. fclose(fp);
  1866. sprintf(command,"chmod a+x %s",bld);
  1867. system(command);
  1868. sprintf(command,"%s/bin/%s",ShortModelName[Model],exec_ppp);
  1869. if (!(fp = fopen(command,"r")))
  1870. {
  1871. sprintf(message,"Building preprocessor ppp");
  1872. BlueMessage(message);
  1873. strcat(bld," ");
  1874. strcat(bld,ShortModelName[Model]);
  1875. strcat(bld,"/bld");
  1876. system(bld);
  1877. }
  1878. else fclose(fp); // Executable exists already
  1879. sprintf(command,"cp %s/bin/%s %s/run\n",ShortModelName[Model],exec_ppp,ShortModelName[Model]);
  1880. system(command);
  1881. return 1; /* Success */
  1882. }
  1883. void FinishLine(void)
  1884. {
  1885. char text[80];
  1886. int OldCores;
  1887. OldCores = Cores;
  1888. if (CursorSel == NULL) return;
  1889. if (CursorSel->type == SEL_INT)
  1890. {
  1891. CursorSel->iv = atoi(CursorSel->teva);
  1892. sprintf(text,"%10d",CursorSel->iv);
  1893. strcpy(CursorSel->teva,text+10-CursorSel->edco);
  1894. if (CursorSel->piv) *CursorSel->piv = CursorSel->iv;
  1895. }
  1896. if (CursorSel->type == SEL_REAL)
  1897. {
  1898. CursorSel->fv = atof(CursorSel->teva);
  1899. FormatReal(CursorSel->fv,text);
  1900. strcpy(CursorSel->teva,text);
  1901. }
  1902. /*
  1903. if (CursorSel == SelMulti) // Enable or disable Lat2
  1904. {
  1905. SelLat2->no = (SelMulti->iv != 2) ;
  1906. }
  1907. */
  1908. if (OldCores == 1 && Cores > 1) ForceRebuild = 1;
  1909. if (OldCores > 1 && Cores == 1) ForceRebuild = 1;
  1910. if (Model == PLASIM && OldCores != Cores) ForceRebuild = 1;
  1911. }
  1912. int CheckPumaNamelist(void)
  1913. {
  1914. int i,safe_ntspd;
  1915. int *ntspd;
  1916. int *nyoden = NULL;
  1917. double s;
  1918. struct SelStruct *Sel;
  1919. FILE *fp;
  1920. FinishLine();
  1921. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1922. {
  1923. if (Sel->piv) *Sel->piv = Sel->iv;
  1924. if (!strcmp(Sel->text,"NTSPD" )) ntspd = &Sel->iv;
  1925. if (!strcmp(Sel->text,"NYODEN" )) nyoden = &Sel->iv;
  1926. if (!strcmp(Sel->text,"Orography" )) nreadsr = Sel->iv;
  1927. if (!strcmp(Sel->text,"Annual cycle")) nac = Sel->iv;
  1928. }
  1929. // Check for resolution defines as T value
  1930. if (Resolution > 0) Latitudes = ResLat[Resolution];
  1931. // Check # of latitudes for correct values (FFT requirements)
  1932. /*
  1933. if (Latitudes >= 2048) Latitudes = 2048; // T1365
  1934. else if (Latitudes >= 1024) Latitudes = 1024; // T682
  1935. else if (Latitudes >= 512) Latitudes = 512; // T341
  1936. else if (Latitudes >= 256) Latitudes = 256; // T170
  1937. else if (Latitudes >= 192) Latitudes = 192; // T127
  1938. else if (Latitudes >= 160) Latitudes = 160; // T106
  1939. else if (Latitudes >= 128) Latitudes = 128; // T85
  1940. else if (Latitudes >= 96) Latitudes = 96; // T63
  1941. else if (Latitudes >= 64) Latitudes = 64; // T42
  1942. else if (Latitudes >= 48) Latitudes = 48; // T31
  1943. else if (Latitudes >= 32) Latitudes = 32; // T21
  1944. else if (Latitudes >= 24) Latitudes = 24; // T15
  1945. else Latitudes = 16; // T10
  1946. */
  1947. if (Debug) printf("Latitudes = %d\n",Latitudes);
  1948. // Force # of levels to 20 for predefined Yoden profiles
  1949. if (nyoden && *nyoden > 0)
  1950. {
  1951. Levels = 20;
  1952. Yoden = *nyoden;
  1953. }
  1954. // Make sure # of CPU's is a power of 2 and in the proper range
  1955. if (Cores > Latitudes/2) Cores = Latitudes/2;
  1956. if (Cores < 1) Cores = 1;
  1957. if ((Latitudes/2) % Cores != 0) Cores = 1;
  1958. // Multirun is currently implemented for two runs only
  1959. if (Multirun >= 2)
  1960. {
  1961. Multirun = SelMulti->iv = 2;
  1962. Cores = SelCPU->iv = 1;
  1963. }
  1964. return 0; /* Success */
  1965. }
  1966. int CheckCATNamelist(void)
  1967. {
  1968. int i,safe_ntspd;
  1969. int *ntspd;
  1970. int *nyoden = NULL;
  1971. double s;
  1972. struct SelStruct *Sel;
  1973. FILE *fp;
  1974. FinishLine();
  1975. /*
  1976. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1977. {
  1978. if (Sel->piv) *Sel->piv = Sel->iv;
  1979. if (!strcmp(Sel->text,"NTSPD" )) ntspd = &Sel->iv;
  1980. if (!strcmp(Sel->text,"NYODEN" )) nyoden = &Sel->iv;
  1981. if (!strcmp(Sel->text,"Orography" )) nreadsr = Sel->iv;
  1982. if (!strcmp(Sel->text,"Annual cycle")) nac = Sel->iv;
  1983. }
  1984. */
  1985. // Check for resolution
  1986. if (Resolution == 1)
  1987. Latitudes = Latitude2 = 64;
  1988. else if (Resolution == 2)
  1989. Latitudes = Latitude2 = 256;
  1990. else if (Resolution == 3)
  1991. Latitudes = Latitude2 = 1024;
  1992. if (Debug) printf("NGX = NGY = %d\n",Latitudes);
  1993. return 0; /* Success */
  1994. }
  1995. int CheckPlasimNamelist(void)
  1996. {
  1997. int i,safe_mpstep;
  1998. int *mpstep;
  1999. double s;
  2000. struct SelStruct *Sel;
  2001. FILE *fp;
  2002. FinishLine();
  2003. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  2004. {
  2005. if (Sel->piv) *Sel->piv = Sel->iv;
  2006. if (!strcmp(Sel->text,"MPSTEP")) mpstep = &Sel->iv;
  2007. if (!strcmp(Sel->text,"Orography" )) nreadsr = Sel->iv;
  2008. if (!strcmp(Sel->text,"Annual cycle")) nac = Sel->iv;
  2009. }
  2010. // Check for resolution defines as T value
  2011. if (Resolution > 0) Latitudes = ResLat[Resolution];
  2012. // LSG works currently only with T21 PlaSim
  2013. if (Lsg)
  2014. {
  2015. Resolution = RES_T21;
  2016. Latitudes = ResLat[RES_T21];
  2017. }
  2018. // Check # of latitudes for correct values (FFT requirements)
  2019. if (Latitudes >= 64) Latitudes = 64; // T42
  2020. else if (Latitudes >= 48) Latitudes = 48; // T31
  2021. else if (Latitudes >= 32) Latitudes = 32; // T21
  2022. else if (Latitudes >= 4) Latitudes = 4; // T2
  2023. else if (Latitudes >= 2) Latitudes = 2; // T1
  2024. // Set mpstep to a safe value
  2025. if (Resolution > 0) *mpstep = PlasimSteps[Resolution];
  2026. else
  2027. {
  2028. s = log(Latitudes / 32.0) / log(2.0);
  2029. safe_mpstep = 45.0 / pow(3.0,s);
  2030. if (*mpstep > safe_mpstep) *mpstep = safe_mpstep;
  2031. }
  2032. // Make sure # of CPU's is a power of 2 and in the proper range
  2033. if (Cores > Latitudes) Cores = Latitudes;
  2034. if (Cores < 1) Cores = 1;
  2035. if (Latitudes % Cores != 0) Cores = 1;
  2036. return 0; /* Success */
  2037. }
  2038. double *ReadGrid(const char *fn, int klat, double Sc)
  2039. {
  2040. int i,j,k,jlev,iteml,gridsize;
  2041. FILE *ifp;
  2042. int head[8];
  2043. double *gp;
  2044. char Line[256];
  2045. char Elem[16];
  2046. nlat = klat;
  2047. nlon = nlat + nlat;
  2048. VGAX = (double)sfbox_w / nlon;
  2049. VGAY = (double)sfbox_h / (nlat-1);
  2050. gridsize = (nlon+1) * nlat * sizeof(double);
  2051. gp = malloc(gridsize);
  2052. ifp = fopen(fn,"r");
  2053. if (!ifp) return NULL;
  2054. fgets(Line,sizeof(Line),ifp);
  2055. for (i = 0 ; i < 8 ; ++i)
  2056. {
  2057. strncpy(Elem,Line+i*10,10);
  2058. Elem[10] = 0;
  2059. head[i] = atoi(Elem);
  2060. }
  2061. if (head[4] == nlon && head[5] == nlat)
  2062. {
  2063. for (j = 0 ; j < nlat ; ++j)
  2064. {
  2065. for (i = 0 ; i < nlon ; i+=8)
  2066. {
  2067. fgets(Line,sizeof(Line),ifp);
  2068. iteml = strlen(Line) / 8;
  2069. for (k=0 ; k < 8 ; ++k)
  2070. {
  2071. strncpy(Elem,Line+k*iteml,iteml);
  2072. Elem[iteml] = 0;
  2073. gp[k+i+j*(nlon+1)] = atof(Elem) * Sc;
  2074. }
  2075. }
  2076. gp[nlon+j*(nlon+1)] = gp[j*(nlon+1)];
  2077. }
  2078. }
  2079. else printf("head[4] = %d head[5] = %d\n",head[4],head[5]);
  2080. fclose(ifp);
  2081. return gp;
  2082. }
  2083. void WriteOroNamelist(FILE *fp)
  2084. {
  2085. int lon1,lon2,lat1,lat2;
  2086. double w,h;
  2087. w = Frame[0].w;
  2088. h = Frame[0].h;
  2089. // Round to next gridpoint
  2090. lon1 = 1 + (Frame[0].xs * Latitudes*2) / w;
  2091. lon2 = 1 +((Frame[0].xs + Frame[0].ws) * Latitudes*2) / w;
  2092. lat1 = 1 + (Frame[0].ys * Latitudes) / h;
  2093. lat2 = 1 +((Frame[0].ys + Frame[0].hs) * Latitudes) / h;
  2094. fprintf(fp," %-8s=%d\n","NSRV" , 0);
  2095. fprintf(fp," %-8s=%d\n","NORO" ,noro);
  2096. fprintf(fp," %-8s=%d\n","OROANO" ,OroAno);
  2097. fprintf(fp," %-8s=%d\n","LON1ORO",lon1);
  2098. fprintf(fp," %-8s=%d\n","LON2ORO",lon2);
  2099. fprintf(fp," %-8s=%d\n","LAT1ORO",lat1);
  2100. fprintf(fp," %-8s=%d\n","LAT2ORO",lat2);
  2101. fprintf(fp," %-8s=%d\n","TGRANO" ,TgrAno);
  2102. lon1 = 1 + (Frame[1].xs * Latitudes*2) / Frame[1].w;
  2103. lon2 = 1 +((Frame[1].xs + Frame[1].ws) * Latitudes*2) / Frame[1].w;
  2104. lat1 = 1 + (Frame[1].ys * Latitudes) / Frame[1].h;
  2105. lat2 = 1 +((Frame[1].ys + Frame[1].hs) * Latitudes) / Frame[1].h;
  2106. fprintf(fp," %-8s=%d\n","LON1TGR",lon1);
  2107. fprintf(fp," %-8s=%d\n","LON2TGR",lon2);
  2108. fprintf(fp," %-8s=%d\n","LAT1TGR",lat1);
  2109. fprintf(fp," %-8s=%d\n","LAT2TGR",lat2);
  2110. return; /* Success */
  2111. }
  2112. void WriteResolutionNamelist(void)
  2113. {
  2114. int i;
  2115. char nln[256];
  2116. FILE *fp;
  2117. for (i = 0 ; i < Multirun ; ++i)
  2118. {
  2119. sprintf(nln,"%s/run/resolution_namelist",ShortModelName[Model]);
  2120. if (Multirun > 1) sprintf(nln+strlen(nln),"_%2.2d",i);
  2121. fp = fopen(nln,"w");
  2122. if (fp == NULL)
  2123. {
  2124. printf("Could not open file <%s> for writing\n",nln);
  2125. return; /* Failure */
  2126. }
  2127. fprintf(fp," &RES\n");
  2128. fprintf(fp," NLAT = %d\n" ,Latitudes);
  2129. if (Model != SAM) fprintf(fp," NLEV = %d\n" ,Levels);
  2130. fprintf(fp," /END\n");
  2131. fclose(fp);
  2132. }
  2133. }
  2134. /* ================= */
  2135. /* WritePumaNamelist */
  2136. /* ================= */
  2137. int WritePumaNamelist(void) /* also used for model SAM */
  2138. {
  2139. int i,j,k,sum,val,imr;
  2140. FILE *fp;
  2141. char backup_name[256];
  2142. char nln[256];
  2143. struct SelStruct *Sel;
  2144. FinishLine();
  2145. // Write file <puma_namelist> or <sam_namelist>
  2146. for (imr = 0 ; imr < Multirun ; ++imr)
  2147. {
  2148. strcpy(nln,ShortModelName[Model]);
  2149. strcat(nln,"/run/");
  2150. strcat(nln,namelist_name);
  2151. if (Multirun > 1) sprintf(nln+strlen(nln),"_%2.2d",imr);
  2152. fp = fopen(nln,"w");
  2153. if (fp == NULL)
  2154. {
  2155. printf("Could not open file <%s> for writing\n",nln);
  2156. return 0; /* Failure */
  2157. }
  2158. fprintf(fp," &%s_nl\n",ShortModelName[Model]);
  2159. if (Preprocessed || noro) nreadsr = 1;
  2160. fprintf(fp," %-8s=%6d\n","NOUTPUT" ,noutput);
  2161. fprintf(fp," %-8s=%6d\n","NGUI" ,ngui);
  2162. if (nac)
  2163. {
  2164. fprintf(fp," %-8s=%11.4f\n","TAC" ,360.0);
  2165. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2166. }
  2167. else
  2168. {
  2169. fprintf(fp," %-8s=%11.4f\n","TAC" , 0.0);
  2170. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2171. }
  2172. if (ngui) fprintf(fp," %-8s=%6d\n","NYEARS",SimYears);
  2173. else fprintf(fp," %-8s=%6d\n","NYEARS",1);
  2174. fprintf(fp," %-8s=%6d\n","NMONTHS",0);
  2175. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2176. {
  2177. if (Sel->type == SEL_INT)
  2178. fprintf(fp," %-8s=%6d\n",Sel->text,Sel->iv);
  2179. if (Sel->type == SEL_REAL)
  2180. fprintf(fp," %-8s=%s\n",Sel->text,Sel->teva);
  2181. }
  2182. // Check for mode selections
  2183. for (i=0,sum=0 ; i < DimSH ; ++i) sum += Ampli[i];
  2184. if (sum != DimSH) // Some modes are off
  2185. {
  2186. fprintf(fp," NSPECSEL = ");
  2187. i = 0;
  2188. while (i < DimSH)
  2189. {
  2190. val = Ampli[i];
  2191. j = i;
  2192. while (j < DimSH && Ampli[j] == val) ++j;
  2193. k = j - i;
  2194. if (k == 1) fprintf(fp,"%d",val);
  2195. else fprintf(fp,"%d*%d",k,val);
  2196. if (j < DimSH) fprintf(fp,",");
  2197. i = j;
  2198. }
  2199. fprintf(fp,"\n");
  2200. }
  2201. fprintf(fp," /END\n");
  2202. fclose(fp);
  2203. }
  2204. return 1; /* Success */
  2205. }
  2206. /* ================ */
  2207. /* WriteCatNamelist */
  2208. /* ================ */
  2209. int WriteCatNamelist(void)
  2210. {
  2211. int i,j,k,sum,val;
  2212. FILE *fp;
  2213. char backup_name[256];
  2214. char nln[256];
  2215. struct SelStruct *Sel;
  2216. FinishLine();
  2217. // Write file <cat_namelist>
  2218. strcpy(nln,ShortModelName[Model]);
  2219. strcat(nln,"/run/");
  2220. strcat(nln,namelist_name);
  2221. fp = fopen(nln,"w");
  2222. if (fp == NULL)
  2223. {
  2224. printf("Could not open file <%s> for writing\n",nln);
  2225. return 0; /* Failure */
  2226. }
  2227. fprintf(fp," &%s_nl\n",ShortModelName[Model]);
  2228. fprintf(fp," %-8s=%6d\n","NGUI" ,ngui);
  2229. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2230. {
  2231. if (Sel->type == SEL_INT)
  2232. fprintf(fp," %-8s=%6d\n",Sel->text,Sel->iv);
  2233. if (Sel->type == SEL_REAL)
  2234. fprintf(fp," %-8s=%s\n",Sel->text,Sel->teva);
  2235. if (!strncmp(Sel->text,"NSIM",4))
  2236. {
  2237. if (Sel->iv > 0) CatSim = Sel->iv;
  2238. }
  2239. }
  2240. fprintf(fp," /END\n");
  2241. fclose(fp);
  2242. return 1; /* Success */
  2243. }
  2244. /* ================ */
  2245. /* WritePPPNamelist */
  2246. /* ================ */
  2247. int WritePPPNamelist(void)
  2248. {
  2249. int i,j,k,sum,val;
  2250. FILE *fp;
  2251. char backup_name[256];
  2252. char nln[256];
  2253. struct SelStruct *Sel;
  2254. FinishLine();
  2255. WriteResolutionNamelist();
  2256. /* 2. Write file <ppp_namelist> */
  2257. strcpy(nln,ShortModelName[Model]);
  2258. strcat(nln,"/run/");
  2259. strcat(nln,"ppp_namelist");
  2260. strcpy(backup_name,nln);
  2261. strcat(backup_name,".bak");
  2262. rename(nln,backup_name);
  2263. fp = fopen(nln,"w");
  2264. if (fp == NULL)
  2265. {
  2266. printf("Could not open file <%s> for writing\n",nln);
  2267. return 0; /* Failure */
  2268. }
  2269. fprintf(fp," &ppp_nl\n");
  2270. if (Preprocessed || noro) nreadsr = 1;
  2271. fprintf(fp," %-8s=%d\n","NOUTPUT" ,noutput);
  2272. fprintf(fp," %-8s=%d\n","NGUI" ,ngui);
  2273. if (nac)
  2274. {
  2275. fprintf(fp," %-8s=%11.4f\n","TAC" ,360.0);
  2276. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2277. }
  2278. else
  2279. {
  2280. fprintf(fp," %-8s=%11.4f\n","TAC" , 0.0);
  2281. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2282. }
  2283. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2284. {
  2285. if (Sel->type == SEL_INT)
  2286. fprintf(fp," %-8s=%6d\n",Sel->text,Sel->iv);
  2287. if (Sel->type == SEL_REAL)
  2288. fprintf(fp," %-8s=%s\n",Sel->text,Sel->teva);
  2289. }
  2290. WriteOroNamelist(fp);
  2291. fprintf(fp," /END\n");
  2292. fclose(fp);
  2293. return 1; /* Success */
  2294. }
  2295. /* ======== */
  2296. /* FillPoly */
  2297. /* ======== */
  2298. void FillPoly(int n, double Poly[])
  2299. {
  2300. int i;
  2301. XPoint xpol[8];
  2302. for (i=0; i < n ; ++i)
  2303. {
  2304. xpol[i].x = OffX + Poly[i+i ] + 0.5;
  2305. xpol[i].y = OffY + Poly[i+i+1] + 0.5;
  2306. }
  2307. XFillPolygon(display,pix,gc,xpol,n,Convex,CoordModeOrigin);
  2308. }
  2309. /* ======= */
  2310. /* IsoArea */
  2311. /* ======= */
  2312. void IsoArea(INT y, double vl, double vh, double Top[], double Bot[], INT Dim)
  2313. {
  2314. INT f,x,p;
  2315. double xl,xr,yt,yb;
  2316. double Poly[16];
  2317. for (x=0 ; x < Dim-1 ; x++)
  2318. {
  2319. Flag[x] = 0;
  2320. if (Top[x ] < vl) Flag[x] |= TOLELO;
  2321. if (Top[x ] >= vh) Flag[x] |= TOLEHI;
  2322. if (Top[x+1] < vl) Flag[x] |= TORILO;
  2323. if (Top[x+1] >= vh) Flag[x] |= TORIHI;
  2324. if (Bot[x ] < vl) Flag[x] |= BOLELO;
  2325. if (Bot[x ] >= vh) Flag[x] |= BOLEHI;
  2326. if (Bot[x+1] < vl) Flag[x] |= BORILO;
  2327. if (Bot[x+1] >= vh) Flag[x] |= BORIHI;
  2328. }
  2329. x = 0;
  2330. while (x < Dim-1)
  2331. {
  2332. xl = VGAX * x ;
  2333. xr = VGAX * (x+1);
  2334. yt = VGAY * y ;
  2335. yb = VGAY * (y+1);
  2336. f = Flag[x];
  2337. if (f == 0)
  2338. {
  2339. x++;
  2340. while (x < Dim-1 && Flag[x] == 0)
  2341. {
  2342. x++;
  2343. xr = VGAX * x;
  2344. }
  2345. Poly[0] = Poly[6] = xl;
  2346. Poly[1] = Poly[3] = yt;
  2347. Poly[2] = Poly[4] = xr;
  2348. Poly[5] = Poly[7] = yb;
  2349. FillPoly(4,Poly);
  2350. }
  2351. else if (f == (TOLELO | TORILO | BOLELO | BORILO) ||
  2352. f == (TOLEHI | TORIHI | BOLEHI | BORIHI)) x++;
  2353. else if (Top[x] < vl && Top[x+1] >= vl && Bot[x] >= vl && Bot[x+1] < vl)
  2354. {
  2355. Poly[1] = Poly[3] = yt;
  2356. Poly[2] = Poly[4] = xr;
  2357. Poly[0] = IPX(Top[x ],vl,Top[x+1]);
  2358. Poly[5] = IPY(Top[x+1],vl,Bot[x+1]);
  2359. FillPoly(3,Poly);
  2360. Poly[1] = Poly[3] = yb;
  2361. Poly[2] = Poly[4] = xl;
  2362. Poly[0] = IPX(Bot[x ],vl,Bot[x+1]);
  2363. Poly[5] = IPY(Top[x ],vl,Bot[x ]);
  2364. FillPoly(3,Poly);
  2365. ++x;
  2366. }
  2367. else if (Top[x] >= vl && Top[x+1] < vl && Bot[x] < vl && Bot[x+1] >= vl)
  2368. {
  2369. Poly[0] = Poly[4] = xl;
  2370. Poly[1] = Poly[3] = yt;
  2371. Poly[2] = IPX(Top[x ],vl,Top[x+1]);
  2372. Poly[5] = IPY(Top[x ],vl,Bot[x ]);
  2373. FillPoly(3,Poly);
  2374. Poly[0] = Poly[2] = xr;
  2375. Poly[3] = Poly[5] = yb;
  2376. Poly[1] = IPY(Top[x+1],vl,Bot[x+1]);
  2377. Poly[4] = IPX(Bot[x ],vl,Bot[x+1]);
  2378. FillPoly(3,Poly);
  2379. ++x;
  2380. }
  2381. else
  2382. {
  2383. p = 0;
  2384. if (Top[x] < vl)
  2385. {
  2386. if (Top[x+1] >= vl)
  2387. {
  2388. Poly[p++] = IPX(Top[x],vl,Top[x+1]);
  2389. Poly[p++] = yt;
  2390. }
  2391. if (Top[x+1] >= vh)
  2392. {
  2393. Poly[p++] = IPX(Top[x],vh,Top[x+1]);
  2394. Poly[p++] = yt;
  2395. }
  2396. }
  2397. else if (Top[x] >= vh)
  2398. {
  2399. if (Top[x+1] < vh)
  2400. {
  2401. Poly[p++] = IPX(Top[x],vh,Top[x+1]);
  2402. Poly[p++] = yt;
  2403. }
  2404. if (Top[x+1] < vl)
  2405. {
  2406. Poly[p++] = IPX(Top[x],vl,Top[x+1]);
  2407. Poly[p++] = yt;
  2408. }
  2409. }
  2410. else
  2411. {
  2412. Poly[p++] = xl;
  2413. Poly[p++] = yt;
  2414. if (Top[x+1] < vl)
  2415. {
  2416. Poly[p++] = IPX(Top[x],vl,Top[x+1]);
  2417. Poly[p++] = yt;
  2418. }
  2419. if (Top[x+1] >= vh)
  2420. {
  2421. Poly[p++] = IPX(Top[x],vh,Top[x+1]);
  2422. Poly[p++] = yt;
  2423. }
  2424. }
  2425. if (Top[x+1] < vl)
  2426. {
  2427. if (Bot[x+1] >= vl)
  2428. {
  2429. Poly[p++] = xr;
  2430. Poly[p++] = IPY(Top[x+1],vl,Bot[x+1]);
  2431. }
  2432. if (Bot[x+1] >= vh)
  2433. {
  2434. Poly[p++] = xr;
  2435. Poly[p++] = IPY(Top[x+1],vh,Bot[x+1]);
  2436. }
  2437. }
  2438. else if (Top[x+1] >= vh)
  2439. {
  2440. if (Bot[x+1] < vh)
  2441. {
  2442. Poly[p++] = xr;
  2443. Poly[p++] = IPY(Top[x+1],vh,Bot[x+1]);
  2444. }
  2445. if (Bot[x+1] < vl)
  2446. {
  2447. Poly[p++] = xr;
  2448. Poly[p++] = IPY(Top[x+1],vl,Bot[x+1]);
  2449. }
  2450. }
  2451. else
  2452. {
  2453. Poly[p++] = xr;
  2454. Poly[p++] = yt;
  2455. if (Bot[x+1] < vl)
  2456. {
  2457. Poly[p++] = xr;
  2458. Poly[p++] = IPY(Top[x+1],vl,Bot[x+1]);
  2459. }
  2460. if (Bot[x+1] >= vh)
  2461. {
  2462. Poly[p++] = xr;
  2463. Poly[p++] = IPY(Top[x+1],vh,Bot[x+1]);
  2464. }
  2465. }
  2466. if (Bot[x+1] < vl)
  2467. {
  2468. if (Bot[x] >= vl)
  2469. {
  2470. Poly[p++] = IPX(Bot[x],vl,Bot[x+1]);
  2471. Poly[p++] = yb;
  2472. }
  2473. if (Bot[x] >= vh)
  2474. {
  2475. Poly[p++] = IPX(Bot[x],vh,Bot[x+1]);
  2476. Poly[p++] = yb;
  2477. }
  2478. }
  2479. else if (Bot[x+1] >= vh)
  2480. {
  2481. if (Bot[x] < vh)
  2482. {
  2483. Poly[p++] = IPX(Bot[x],vh,Bot[x+1]);
  2484. Poly[p++] = yb;
  2485. }
  2486. if (Bot[x] < vl)
  2487. {
  2488. Poly[p++] = IPX(Bot[x],vl,Bot[x+1]);
  2489. Poly[p++] = yb;
  2490. }
  2491. }
  2492. else
  2493. {
  2494. Poly[p++] = xr;
  2495. Poly[p++] = yb;
  2496. if (Bot[x] < vl)
  2497. {
  2498. Poly[p++] = IPX(Bot[x],vl,Bot[x+1]);
  2499. Poly[p++] = yb;
  2500. }
  2501. if (Bot[x] >= vh)
  2502. {
  2503. Poly[p++] = IPX(Bot[x],vh,Bot[x+1]);
  2504. Poly[p++] = yb;
  2505. }
  2506. }
  2507. if (Bot[x] < vl)
  2508. {
  2509. if (Top[x] >= vl)
  2510. {
  2511. Poly[p++] = xl;
  2512. Poly[p++] = IPY(Top[x],vl,Bot[x]);
  2513. }
  2514. if (Top[x] >= vh)
  2515. {
  2516. Poly[p++] = xl;
  2517. Poly[p++] = IPY(Top[x],vh,Bot[x]);
  2518. }
  2519. }
  2520. else if (Bot[x] >= vh)
  2521. {
  2522. if (Top[x] < vh)
  2523. {
  2524. Poly[p++] = xl;
  2525. Poly[p++] = IPY(Top[x],vh,Bot[x]);
  2526. }
  2527. if (Top[x] < vl)
  2528. {
  2529. Poly[p++] = xl;
  2530. Poly[p++] = IPY(Top[x],vl,Bot[x]);
  2531. }
  2532. }
  2533. else
  2534. {
  2535. Poly[p++] = xl;
  2536. Poly[p++] = yb;
  2537. if (Top[x] < vl)
  2538. {
  2539. Poly[p++] = xl;
  2540. Poly[p++] = IPY(Top[x],vl,Bot[x]);
  2541. }
  2542. if (Top[x] >= vh)
  2543. {
  2544. Poly[p++] = xl;
  2545. Poly[p++] = IPY(Top[x],vh,Bot[x]);
  2546. }
  2547. }
  2548. FillPoly(p>>1,Poly);
  2549. x++;
  2550. }
  2551. }
  2552. }
  2553. /* ======== */
  2554. /* IsoAreas */
  2555. /* ======== */
  2556. void IsoAreas(double *Field, int DimX, int DimY, struct ColorStrip Strip[])
  2557. {
  2558. INT i;
  2559. INT y;
  2560. double *Top;
  2561. double *Bot;
  2562. i = 0;
  2563. while (Strip[i].Name)
  2564. {
  2565. Top = Field;
  2566. Bot = Field + DimX;
  2567. XSetForeground(display,gc,Strip[i].pixel);
  2568. for (y = 0 ; y < DimY-1 ; y++)
  2569. {
  2570. IsoArea(y,Strip[i].Lo,Strip[i].Hi,Top,Bot,DimX);
  2571. Top += DimX;
  2572. Bot += DimX;
  2573. }
  2574. ++i;
  2575. }
  2576. }
  2577. /* ========= */
  2578. /* CalcFrame */
  2579. /* ========= */
  2580. int CalcFrame(int lats)
  2581. {
  2582. // int i,l,x,y;
  2583. // double *Grid;
  2584. //
  2585. // nlat = lats;
  2586. // nlon = lats * 2;
  2587. // VGAX = (sfbox_w - 1.0) / nlon;
  2588. // VGAY = (sfbox_h - 1.0) / (nlat-1);
  2589. //
  2590. // if (!Flag) Flag = malloc((nlon+1) * sizeof(int));
  2591. // if (!Frame[0].Grid) Frame[0].Grid = malloc(nlon * nlat * nlev * sizeof(double));
  2592. // if (!Frame[0].pixmap) Frame[0].pixmap = XCreatePixmap(display,Cow,sfbox_w,sfbox_h,ScreenD);
  2593. // pix = Frame[0].pixmap;
  2594. // Grid = Frame[0].Grid;
  2595. // IsoAreas(Grid,nlon+1,nlat,Frame[0].Strip);
  2596. return 0;
  2597. }
  2598. Pixmap IsoPixmap(double *Grid, int nlon, int nlat, struct ColorStrip Strip[])
  2599. {
  2600. pix = XCreatePixmap(display,Cow,sfbox_w,sfbox_h,ScreenD);
  2601. //XFillRectangle(display,Cow,gc,0,0,Frame[0].w,Frame[0].h);
  2602. Flag = realloc(Flag,(nlon+1) * sizeof(int));
  2603. IsoAreas(Grid,nlon+1,nlat,Strip);
  2604. //XSetForeground(display,gc,Blue.pixel);
  2605. //XDrawRectangle(display,Cow,gc,Frame[0].x-1,Frame[0].y-1,Frame[0].w,Frame[0].h);
  2606. return pix;
  2607. }
  2608. int PPPCompiled(void)
  2609. {
  2610. FILE *fp;
  2611. char fn[128];
  2612. strcpy(fn,"puma/run/");
  2613. strcat(fn,exec_ppp);
  2614. if ((fp = fopen(fn,"r")))
  2615. {
  2616. fclose(fp);
  2617. return 1;
  2618. }
  2619. return 0;
  2620. }
  2621. void ShowOrography(void)
  2622. {
  2623. int i,m,n,k,l,x,y,h,w,lon1,lon2,lat1,lat2,tx,ty,dx;
  2624. int r,len,width,height,xp,yp;
  2625. char Text[80];
  2626. double flon,flat;
  2627. XSetFont(display, gc, FixFont->fid);
  2628. XSetForeground(display,gc,TextC);
  2629. XSetBackground(display,gc,WinBG);
  2630. if (Model == PLASIM)
  2631. {
  2632. if (Planet == MARS)
  2633. {
  2634. XCopyArea(display,OpmMars,Cow,gc,0,0,
  2635. Frame[0].w,Frame[0].h,Frame[0].x,Frame[0].y);
  2636. }
  2637. else if (Planet == EARTH)
  2638. {
  2639. XCopyArea(display,OpmEarth,Cow,gc,0,0,
  2640. Frame[0].w,Frame[0].h,Frame[0].x,Frame[0].y);
  2641. }
  2642. return;
  2643. }
  2644. k = 0; // Frame number 0 reserved for topography
  2645. if (noro == 0) // No orography
  2646. {
  2647. XSetForeground(display,gc,Blue.pixel);
  2648. XFillRectangle(display,Cow,gc,Frame[k].x-1,Frame[k].y-1,Frame[k].w,Frame[k].h);
  2649. XSetForeground(display,gc,WhitePix);
  2650. XSetBackground(display,gc,Blue.pixel);
  2651. l = strlen("Aqua Planet Setup");
  2652. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2653. ty = Frame[k].y + Frame[k].h / 2 - FixFontHeight;
  2654. XDrawImageString(display,Cow,gc,tx,ty,"Aqua Planet Setup",l);
  2655. l = strlen("To change mark option 'Orography'");
  2656. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2657. ty += FixFontHeight;
  2658. XDrawImageString(display,Cow,gc,tx,ty,"To change mark option 'Orography'",l);
  2659. XSetBackground(display,gc,WinBG);
  2660. }
  2661. else
  2662. {
  2663. XCopyArea(display,OpmPrep,Cow,gc,0,0,Frame[0].w,Frame[0].h,Frame[0].x,Frame[0].y);
  2664. XDrawRectangle(display,Cow,gc,Frame[k].x-1,Frame[k].y-1,Frame[k].w,Frame[k].h);
  2665. l = strlen(Frame[k].t[0]);
  2666. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2667. ty = Frame[k].y - FixFont->descent - 2;
  2668. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[0],l);
  2669. }
  2670. if (noro && (Button1Down || (Frame[k].ws && Frame[k].hs)))
  2671. {
  2672. x = Frame[k].x+Frame[k].xs;
  2673. y = Frame[k].y+Frame[k].ys;
  2674. w = Frame[k].ws;
  2675. h = Frame[k].hs;
  2676. if (w < 0) {x += w; w = -w;}
  2677. if (h < 0) {y += h; h = -h;}
  2678. XSetForeground(display,gc,WhitePix);
  2679. XDrawArc(display,Cow,gc,x,y,w,h,0,FULLARC);
  2680. XSetForeground(display,gc,BlackPix);
  2681. XDrawArc(display,Cow,gc,x-1,y-1,w+2,h+2,0,FULLARC);
  2682. flon = 360.0 / Frame[k].w;
  2683. flat = 180.0 / Frame[k].h;
  2684. lon1 = flon * (x - Frame[k].x);
  2685. lon2 = lon1 + flon * w;
  2686. lat1 = 90.0 - flat * (y - Frame[k].y);
  2687. lat2 = lat1 - flat * h;
  2688. sprintf(Frame[k].t[1],"Lon: (%3d:%3d) Lat: (%3d:%3d)",lon1,lon2,lat2,lat1);
  2689. l = strlen(Frame[k].t[1]);
  2690. tx = Frame[k].x;
  2691. ty = Frame[k].y + Frame[k].h + FixFont->ascent;
  2692. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[1],l);
  2693. XSetForeground(display,gc,WhitePix);
  2694. sprintf(Frame[k].t[1],"Mouse marks area ");
  2695. l = strlen(Frame[k].t[1]);
  2696. tx = Frame[k].x;
  2697. ty = Frame[k].y + Frame[k].h + FixFont->ascent;
  2698. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[1],l);
  2699. }
  2700. }
  2701. void ShowMars(void)
  2702. {
  2703. if (Debug) printf("ShowMars\n");
  2704. XPutImage(display,Cow,gc,MapLRM.X,0,0,Frame[1].x,Frame[1].y,MapLRM.w,MapLRM.h);
  2705. }
  2706. void ShowExo(void)
  2707. {
  2708. XPutImage(display,Cow,gc,MapLRL.X,0,0,Frame[0].x,Frame[0].y,MapLRL.w,MapLRL.h);
  2709. XPutImage(display,Cow,gc,MapLRK.X,0,0,Frame[1].x,Frame[1].y,MapLRK.w,MapLRK.h);
  2710. }
  2711. void ShowEarth(void)
  2712. {
  2713. if (Debug) printf("ShowEarth\n");
  2714. XPutImage(display,Cow,gc,MapLRE.X,0,0,Frame[1].x,Frame[1].y,MapLRE.w,MapLRE.h);
  2715. }
  2716. void ShowModeSelector(void)
  2717. {
  2718. int k,dx,r,m,i,n,len,width,height,xp,yp,l,tx,ty;
  2719. char Text[80];
  2720. if (Debug) printf("ShowModeSelector\n");
  2721. k = 1; // Frame number 1 reserved for mode display
  2722. pix = Frame[k].pixmap;
  2723. strcpy(Frame[1].t[0],"Spherical Harmonics mode selector");
  2724. dx = dxsh;
  2725. r = (dx+dx) / 3;
  2726. XSetForeground(display,gc,BlackPix);
  2727. XFillRectangle(display,pix,gc,0,0,Frame[k].w,Frame[k].h);
  2728. for (m=0,i=0 ; m <= DimTr ; ++m)
  2729. {
  2730. for (n=m ; n <= DimTr ; ++n,++i)
  2731. {
  2732. if (Ampli[i]) XSetForeground(display,gc,Green.pixel);
  2733. else XSetForeground(display,gc, Red.pixel);
  2734. XFillArc(display,pix,gc,ModeX[i]-dxs2,ModeY[i]-dxs2,r,r,0,360*64);
  2735. }
  2736. }
  2737. /* Draw mode legend */
  2738. XSetForeground(display,gc,Yellow.pixel);
  2739. XSetBackground(display,gc,BlackPix);
  2740. XSetFont(display, gc, ModFont->fid);
  2741. for (i=0 ; i < 21 ; i+=2)
  2742. {
  2743. sprintf(Text,"%d",i);
  2744. len = strlen(Text);
  2745. width = XTextWidth(ModFont,Text,len);
  2746. height = ModFont->ascent + ModFont->descent;
  2747. xp = dx + i * dx - width/2 - ModFontWidth/2 + 1;
  2748. yp = ModFontHeight;
  2749. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2750. }
  2751. strcpy(Text,"PUMA T21 only!");
  2752. len = strlen(Text);
  2753. xp = dx - ModFontWidth/2 + 1;
  2754. yp = 2 * ModFontHeight + 10 * dx;
  2755. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2756. XSetForeground(display,gc,WhitePix);
  2757. strcpy(Text,"MB 1: Toggle mode");
  2758. len = strlen(Text);
  2759. yp += 2*dx;
  2760. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2761. strcpy(Text,"MB 2: Toggle column");
  2762. len = strlen(Text);
  2763. yp += dx;
  2764. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2765. strcpy(Text,"MB 3: Toggle row");
  2766. len = strlen(Text);
  2767. yp += dx;
  2768. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2769. XSetForeground(display,gc,Yellow.pixel);
  2770. strcpy(Text,"n : Total Wavenumber");
  2771. len = strlen(Text);
  2772. xp = dx - ModFontWidth/2 + 1;
  2773. yp = 2 * ModFontHeight + 18 * dx;
  2774. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2775. XSetForeground(display,gc,LightBlue.pixel);
  2776. for (i=0 ; i < 21 ; i+=2)
  2777. {
  2778. sprintf(Text,"%d",i);
  2779. len = strlen(Text);
  2780. width = XTextWidth(ModFont,Text,len);
  2781. height = ModFont->ascent + ModFont->descent;
  2782. xp = Frame[k].w - width - 2;
  2783. yp = 2 * ModFontHeight + i * dx;
  2784. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2785. }
  2786. strcpy(Text,"m : Zonal Wavenumber");
  2787. len = strlen(Text);
  2788. xp = dx - ModFontWidth/2 + 1;
  2789. yp = 2 * ModFontHeight + 19 * dx;
  2790. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2791. XSetForeground(display,gc,Green.pixel);
  2792. strcpy(Text,"Mode is on");
  2793. len = strlen(Text);
  2794. xp = dx - ModFontWidth/2 + 1;
  2795. yp = ModFontHeight + 16 * dx;
  2796. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2797. xp += dx;
  2798. yp += dx;
  2799. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2800. XSetForeground(display,gc,Red.pixel);
  2801. strcpy(Text,"Mode is off");
  2802. len = strlen(Text);
  2803. xp = dx - ModFontWidth/2 + 1;
  2804. yp = ModFontHeight + 17 * dx;
  2805. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2806. xp += dx;
  2807. yp += dx;
  2808. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2809. XSetForeground(display,gc,WhitePix);
  2810. strcpy(Text,"Switch all modes on");
  2811. len = strlen(Text);
  2812. xp = dx - ModFontWidth/2 + 1;
  2813. yp = ModFontHeight + 20 * dx;
  2814. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2815. xp += dx;
  2816. yp += dx;
  2817. XSetForeground(display,gc,Green.pixel);
  2818. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2819. XSetForeground(display,gc,WhitePix);
  2820. strcpy(Text,"Switch all modes off");
  2821. len = strlen(Text);
  2822. xp = dx - ModFontWidth/2 + 1;
  2823. yp = ModFontHeight + 21 * dx;
  2824. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2825. xp += dx;
  2826. yp += dx;
  2827. XSetForeground(display,gc,Red.pixel);
  2828. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2829. XCopyArea(display,Frame[k].pixmap,Cow,gc,0,0,Frame[k].w,Frame[k].h,Frame[k].x,Frame[k].y);
  2830. XSetForeground(display,gc,Cyan.pixel);
  2831. l = strlen(Frame[k].t[0]);
  2832. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2833. ty = Frame[k].y - FixFont->descent - 2;
  2834. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[0],l);
  2835. XSetForeground(display,gc,LightBlue.pixel);
  2836. l = strlen(Frame[k].t[1]);
  2837. if (l)
  2838. {
  2839. tx = Frame[k].x + (3 * ModFontWidth) / 2;
  2840. ty = Frame[k].y + Frame[k].h + FixFont->ascent;
  2841. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[1],l);
  2842. }
  2843. }
  2844. void ShowFrame1(void)
  2845. {
  2846. if (Model == PLASIM)
  2847. {
  2848. if (Planet == MARS) ShowMars();
  2849. else if (Planet == EXO ) ShowExo();
  2850. else ShowEarth();
  2851. }
  2852. else ShowModeSelector();
  2853. }
  2854. void PreProcess(void)
  2855. {
  2856. FinishLine();
  2857. if (Model == PUMA)
  2858. {
  2859. if (CheckPumaNamelist()) return;
  2860. GenerateNames();
  2861. WritePPPNamelist();
  2862. WritePumaNamelist();
  2863. if (!PPPCompiled()) BuildPPP();
  2864. WriteRunPPP();
  2865. Preprocessed = 1;
  2866. if (Latitudes < 1000)
  2867. sprintf(oro_name,"puma/run/N%3.3d_surf_0129.sra",Latitudes);
  2868. else
  2869. sprintf(oro_name,"puma/run/N%d_surf_0129.sra",Latitudes);
  2870. OroPrep = ReadGrid(oro_name,Latitudes,RevGra);
  2871. OpmPrep = IsoPixmap(OroPrep,2*Latitudes,Latitudes,OroStrip);
  2872. free(OroPrep);
  2873. OroClear = 0;
  2874. }
  2875. else
  2876. {
  2877. RedMessage("Preprocessing is for PUMA only");
  2878. sleep(5);
  2879. }
  2880. }
  2881. void ClearOro(void)
  2882. {
  2883. OroClear = 1;
  2884. OroAqua = 1;
  2885. OroAno = 0;
  2886. PreProcess();
  2887. }
  2888. void WriteNamelistFile(char *nl, int instance)
  2889. {
  2890. char *shomo = ShortModelName[Model];
  2891. struct SelStruct *Sel;
  2892. char fn[256];
  2893. FILE *fp;
  2894. if (Multirun > 1)
  2895. sprintf(fn,"%s/run/%s_namelist_%2.2d",shomo,nl,instance);
  2896. else
  2897. sprintf(fn,"%s/run/%s_namelist",shomo,nl);
  2898. fp = fopen(fn,"w");
  2899. if (fp == NULL)
  2900. {
  2901. printf("Could not open file <%s> for writing\n",fn);
  2902. return; /* Failure */
  2903. }
  2904. // Write namelist file
  2905. fprintf(fp," &%s_nl\n",nl);
  2906. // Write special parameters
  2907. if (!strcmp(nl,"icemod"))
  2908. {
  2909. fprintf(fp," %-12s=%6d\n","NICE",Ice);
  2910. }
  2911. if (!strcmp(nl,"oceanmod"))
  2912. {
  2913. fprintf(fp," %-12s=%6d\n","NOCEAN",Oce);
  2914. //FL0318
  2915. if (Lsg)
  2916. {
  2917. fprintf(fp," %-12s=%6d\n","NLSG" ,Lsg);
  2918. }
  2919. }
  2920. if (!strcmp(nl,"plasim"))
  2921. {
  2922. fprintf(fp," %-12s=%6d\n","NOUTPUT",noutput);
  2923. fprintf(fp," %-12s=%6d\n","NGUI" ,ngui);
  2924. fprintf(fp," %-12s=%6d\n","N_START_YEAR",SimStart);
  2925. if (Lsg) fprintf(fp," %-12s=%6d\n","N_DAYS_PER_YEAR",360);
  2926. else if (Planet != MARS) fprintf(fp," %-12s=%6d\n","N_DAYS_PER_YEAR",365);
  2927. if (ngui) fprintf(fp," %-12s=%6d\n","N_RUN_YEARS",SimYears);
  2928. else fprintf(fp," %-12s=%6d\n","N_RUN_YEARS",1);
  2929. fprintf(fp," %-12s=%6d\n","N_RUN_MONTHS",0);
  2930. fprintf(fp," %-12s=%6d\n","N_RUN_DAYS",0);
  2931. }
  2932. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2933. {
  2934. if (Sel->Item && !strcasecmp(Sel->Item->list,nl))
  2935. {
  2936. if (Sel->type == SEL_TEVA)
  2937. fprintf(fp," %-12s=%c%s%c\n",Sel->text,'\"',Sel->teva,'\"');
  2938. else
  2939. fprintf(fp," %-12s=%s\n",Sel->text,Sel->teva);
  2940. }
  2941. }
  2942. fprintf(fp," /END\n");
  2943. fclose(fp);
  2944. }
  2945. /* =================== */
  2946. /* WritePlasimNamelist */
  2947. /* =================== */
  2948. void WritePlasimNamelist(void)
  2949. {
  2950. int imr;
  2951. FinishLine();
  2952. for (imr = 0 ; imr < Multirun ; ++imr)
  2953. {
  2954. WriteNamelistFile("fluxmod" ,imr);
  2955. WriteNamelistFile("icemod" ,imr);
  2956. WriteNamelistFile("landmod" ,imr);
  2957. WriteNamelistFile("vegmod" ,imr);
  2958. WriteNamelistFile("miscmod" ,imr);
  2959. WriteNamelistFile("oceanmod",imr);
  2960. WriteNamelistFile("planet" ,imr);
  2961. WriteNamelistFile("plasim" ,imr);
  2962. WriteNamelistFile("radmod" ,imr);
  2963. WriteNamelistFile("rainmod" ,imr);
  2964. WriteNamelistFile("seamod" ,imr);
  2965. WriteNamelistFile("surfmod" ,imr);
  2966. }
  2967. }
  2968. void BuildScripts(void)
  2969. {
  2970. if (Model == PUMA)
  2971. {
  2972. if (CheckPumaNamelist()) return;
  2973. GenerateNames();
  2974. WritePumaNamelist();
  2975. if (!Build(PUMA)) Exit();
  2976. WriteRunScript(PUMA);
  2977. }
  2978. if (Model == SAM)
  2979. {
  2980. if (CheckPumaNamelist()) return;
  2981. GenerateNames();
  2982. WritePumaNamelist();
  2983. if (!Build(SAM)) Exit();
  2984. WriteRunScript(SAM);
  2985. }
  2986. if (Model == CAT)
  2987. {
  2988. if (CheckCATNamelist()) return;
  2989. GenerateNames();
  2990. WriteCatNamelist();
  2991. if (!Build(CAT)) Exit();
  2992. WriteRunScript(CAT);
  2993. }
  2994. if (Model == PLASIM)
  2995. {
  2996. CheckPlasimNamelist();
  2997. GenerateNames();
  2998. WritePlasimNamelist();
  2999. if (!Build(PLASIM)) Exit();
  3000. WriteRunScript(PLASIM);
  3001. }
  3002. }
  3003. void SaveExit(void)
  3004. {
  3005. BuildScripts();
  3006. if ((noro || Yoden) && !Preprocessed && Model == PUMA) PreProcess();
  3007. Exit();
  3008. }
  3009. void SaveRun(void)
  3010. {
  3011. char command[256];
  3012. BuildScripts();
  3013. if ((noro || Yoden) && !Preprocessed && Model == PUMA) PreProcess();
  3014. sprintf(command,"%s/run/%s %s/run &",ShortModelName[Model],run_name,ShortModelName[Model]);
  3015. system(command);
  3016. printf("\n=== Success: Launched process %s ===\n\n",run_name);
  3017. Exit();
  3018. }
  3019. void MarkRectangle(void)
  3020. {
  3021. if (Model != PUMA) return;
  3022. if (WinEvent.type == ButtonPress)
  3023. {
  3024. Frame[FrameNo].xs = WinEvent.xbutton.x - Frame[FrameNo].x;
  3025. Frame[FrameNo].ys = WinEvent.xbutton.y - Frame[FrameNo].y;
  3026. Frame[FrameNo].ws = 0;
  3027. Frame[FrameNo].hs = 0;
  3028. }
  3029. if (WinEvent.type == ButtonRelease)
  3030. {
  3031. Frame[FrameNo].ws = WinEvent.xbutton.x - Frame[FrameNo].x - Frame[FrameNo].xs;
  3032. Frame[FrameNo].hs = WinEvent.xbutton.y - Frame[FrameNo].y - Frame[FrameNo].ys;
  3033. }
  3034. if (WinEvent.type == MotionNotify && Button1Down)
  3035. {
  3036. Frame[FrameNo].ws = WinEvent.xbutton.x - Frame[FrameNo].x - Frame[FrameNo].xs;
  3037. Frame[FrameNo].hs = WinEvent.xbutton.y - Frame[FrameNo].y - Frame[FrameNo].ys;
  3038. }
  3039. }
  3040. /* ==================================================== */
  3041. /* AzimuthalImage - Display map in azimuthal projection */
  3042. /* ==================================================== */
  3043. void AzimuthalImage(struct MapImageStruct *s, struct MapImageStruct *d)
  3044. {
  3045. int lam; // lambda pixel coordinate in source image
  3046. int phi; // phi pixel coordinate in source image
  3047. int x ; // x pixel coordinate in destination image
  3048. int y ; // y pixel coordinate in destination image
  3049. int dxc; // pixel coordinate of centre position
  3050. int dxl; // pixel coordinate of left most position
  3051. int dxr; // pixel coordinate of right most position
  3052. int dx ; // centre relative x position
  3053. int dy ; // centre relative y position
  3054. int p00; // euqator
  3055. int l00; // reference longitude
  3056. unsigned int dih; // destination image height
  3057. unsigned int diw; // destination image width
  3058. unsigned int dpw; // destination image padded width
  3059. double rad; // pixel radius of new image
  3060. double ysc; // yscale = source height / destination height
  3061. double rho; // distance from centre
  3062. double xpi; // x scale factor = 2 * PI / width
  3063. double ypi; // y scale factor = PI / height
  3064. double xrf;
  3065. double yrf;
  3066. XImage *sX; // source image
  3067. XImage *dX; // destination image
  3068. // Destroy old image structure inclusive data storage
  3069. if (d->X) XDestroyImage(d->X);
  3070. // Set width of new image
  3071. diw = d->w;
  3072. // Pad width of new image to a multiple of 8
  3073. dpw = (diw + 7) & 0xFFF8;
  3074. // Set height of new image
  3075. dih = d->h;
  3076. // Allocate space for image data
  3077. d->d = calloc(dpw * dih,4);
  3078. // Create image structure
  3079. dX = d->X = XCreateImage(display,CopyFromParent,ScreenD,ZPixmap,0,d->d,dpw,dih,8,0);
  3080. sX = s->X;
  3081. p00 = s->h >> 1;
  3082. rad = dih >> 1;
  3083. dxc = diw >> 1;
  3084. dxl = MAX(dxc - rad, 0);
  3085. dxr = MIN(dxc + rad, diw);
  3086. xpi = s->w / M_PI * 0.5;
  3087. ypi = s->h / M_PI;
  3088. ysc = (double)s->h / (double)dih;
  3089. l00 = (int)((d->l * s->w) / 360 + s->w/2) % s->w;
  3090. xrf = (double)diw / pow(2.0,31),
  3091. yrf = (double)dih / pow(2.0,31);
  3092. // Paint some stars on the sky
  3093. srandom(Seed);
  3094. for (y = 0 ; y < (diw * dih) >> 8 ; ++y)
  3095. XPutPixel(dX,xrf*random(),yrf*random(),WhitePix);
  3096. for (y = 0 ; y < dih ; ++y)
  3097. {
  3098. dy = y - rad;
  3099. phi = y * ysc;
  3100. for (x = dxl ; x < dxr ; ++x)
  3101. {
  3102. dx = x - dxc;
  3103. rho = sqrt(dx * dx + dy * dy);
  3104. if (rho < rad)
  3105. {
  3106. lam = l00 + xpi * atan2(dx / rad, cos(asin(rho / rad)));
  3107. phi = p00 + ypi * asin(dy / rad);
  3108. XPutPixel(dX,x,y,XGetPixel(sX,lam,phi));
  3109. }
  3110. }
  3111. }
  3112. }
  3113. /* =============================================== */
  3114. /* RectImage - Display map in azimuthal projection */
  3115. /* =============================================== */
  3116. void RectImage(struct MapImageStruct *s, struct MapImageStruct *d)
  3117. {
  3118. int x ; // x pixel coordinate in destination image
  3119. int y ; // y pixel coordinate in destination image
  3120. unsigned int dih; // destination image height
  3121. unsigned int diw; // destination image width
  3122. unsigned int dpw; // destination image padded width
  3123. XImage *sX; // source image
  3124. XImage *dX; // destination image
  3125. // Destroy old image structure inclusive data storage
  3126. if (d->X) XDestroyImage(d->X);
  3127. // Set width of new image
  3128. diw = d->w;
  3129. // Pad width of new image to a multiple of 8
  3130. dpw = (diw + 7) & 0xFFF8;
  3131. // Set height of new image
  3132. dih = d->h;
  3133. // Allocate space for image data
  3134. d->d = calloc(dpw * dih,4);
  3135. // Create image structure
  3136. dX = d->X = XCreateImage(display,CopyFromParent,ScreenD,ZPixmap,0,d->d,dpw,dih,8,0);
  3137. sX = s->X;
  3138. for (y = 0 ; y < dih ; ++y)
  3139. {
  3140. for (x = 0 ; x < diw ; ++x)
  3141. {
  3142. XPutPixel(dX,x,y,XGetPixel(sX,x,y));
  3143. }
  3144. }
  3145. }
  3146. void ToggleMode(void)
  3147. {
  3148. int i,j,m,n,mx,my,dx,dy;
  3149. if (Model == PLASIM) // Rotate image
  3150. {
  3151. if (WinEvent.xbutton.button == Button1) // Rotate right
  3152. {
  3153. if (Planet == MARS)
  3154. {
  3155. MapLRM.l += 10;
  3156. if (MapLRM.l > 180) MapLRM.l -= 360;
  3157. AzimuthalImage(&MapHRM,&MapLRM);
  3158. }
  3159. else
  3160. {
  3161. MapLRE.l += 10;
  3162. if (MapLRE.l > 180) MapLRE.l -= 360;
  3163. AzimuthalImage(&MapHRE,&MapLRE);
  3164. }
  3165. }
  3166. else if (WinEvent.xbutton.button == Button3) // Rotate left
  3167. {
  3168. if (Planet == MARS)
  3169. {
  3170. MapLRM.l -= 10;
  3171. if (MapLRM.l < -180) MapLRM.l += 360;
  3172. AzimuthalImage(&MapHRM,&MapLRM);
  3173. }
  3174. else
  3175. {
  3176. MapLRE.l -= 10;
  3177. if (MapLRE.l < -180) MapLRE.l += 360;
  3178. AzimuthalImage(&MapHRE,&MapLRE);
  3179. }
  3180. }
  3181. return;
  3182. }
  3183. if (WinEvent.type == ButtonPress)
  3184. {
  3185. mx = WinEvent.xbutton.x - Frame[FrameNo].x;
  3186. my = WinEvent.xbutton.y - Frame[FrameNo].y;
  3187. for (i=0 ; i < DimSE ; ++i)
  3188. {
  3189. dx = mx - ModeX[i];
  3190. dy = my - ModeY[i];
  3191. if ((dx*dx + dy*dy) < ModeRadiusSq) break;
  3192. }
  3193. if (WinEvent.xbutton.button == Button1)
  3194. {
  3195. if (i == DimSH ) // All modes on
  3196. for (j=0 ; j < DimSH ; ++j) Ampli[j] = 1;
  3197. else if (i == DimSH+1) // All modes off
  3198. for (j=0 ; j < DimSH ; ++j) Ampli[j] = 0;
  3199. else Ampli[i] = 1 - Ampli[i];
  3200. }
  3201. else if (WinEvent.xbutton.button == Button2 && i < DimSH)
  3202. {
  3203. n = ModeN[i];
  3204. for (j=0 ; j < DimSH ; ++j)
  3205. if (ModeN[j] == n) Ampli[j] = 1 - Ampli[j];
  3206. }
  3207. else if (WinEvent.xbutton.button == Button3 && i < DimSH)
  3208. {
  3209. m = ModeM[i];
  3210. for (j=0 ; j < DimSH ; ++j)
  3211. if (ModeM[j] == m) Ampli[j] = 1 - Ampli[j];
  3212. }
  3213. }
  3214. if (WinEvent.type == MotionNotify)
  3215. {
  3216. mx = WinEvent.xbutton.x - Frame[FrameNo].x;
  3217. my = WinEvent.xbutton.y - Frame[FrameNo].y;
  3218. for (i=0 ; i < DimSH ; ++i)
  3219. {
  3220. dx = mx - ModeX[i];
  3221. dy = my - ModeY[i];
  3222. if ((dx*dx + dy*dy) < ModeRadiusSq)
  3223. {
  3224. sprintf(Frame[1].t[1],"Mode (%2d:%2d)",ModeN[i],ModeM[i]);
  3225. return;
  3226. }
  3227. }
  3228. Frame[1].t[1][0] = 0;
  3229. }
  3230. return;
  3231. }
  3232. void InitFrames(void)
  3233. {
  3234. int i,j,n;
  3235. int moselw;
  3236. FILE *ftp;
  3237. /* Size and position of orography window */
  3238. sfbox_w = 360;
  3239. sfbox_h = 180;
  3240. sfbox_x = WINDOW_WIDTH - sfbox_w - 3 * FixFontWidth;
  3241. sfbox_y = nlbox_y + 4;
  3242. /* Parameter for default orographies */
  3243. nlat = 64;
  3244. nlon = nlat * 2;
  3245. VGAX = (sfbox_w - 1.0) / nlon;
  3246. VGAY = (sfbox_h - 1.0) / (nlat-1);
  3247. /* Read T42 orography for Earth and Mars */
  3248. OroEarth = ReadGrid("plasim/dat/T42/N064_surf_0129.sra",nlat,RevGra);
  3249. OroMars = ReadGrid("plasim/dat/T42_mars/N064_surf_0129.sra",nlat,1.0/3.74);
  3250. OpmEarth = IsoPixmap(OroEarth,nlon,nlat,OroStrip);
  3251. OpmPrep = IsoPixmap(OroEarth,nlon,nlat,OroStrip);
  3252. OpmMars = IsoPixmap(OroMars ,nlon,nlat,OroMarsStrip);
  3253. free(OroEarth);
  3254. free(OroMars);
  3255. /* Check for orography in run directory */
  3256. ftp = fopen(oro_name,"r");
  3257. if (ftp) fclose(ftp);
  3258. else strcpy(oro_name,"puma/dat/N064_surf_0129.sra");
  3259. // Orography
  3260. n = 0;
  3261. Frame[n].x = sfbox_x;
  3262. Frame[n].y = sfbox_y + FixFontHeight;
  3263. Frame[n].w = sfbox_w;
  3264. Frame[n].h = sfbox_h;
  3265. Frame[n].b = DarkGreen.pixel;
  3266. Frame[n].f = WhitePix;
  3267. strcpy(Frame[n].t[0],"Orography");
  3268. strcpy(Frame[n].t[1],"64 x 32");
  3269. strcpy(Frame[n].t[2],"");
  3270. Frame[n].Action = MarkRectangle;
  3271. Frame[n].xs = 110;
  3272. Frame[n].ys = 95;
  3273. Frame[n].ws = 60;
  3274. Frame[n].hs = 40;
  3275. SelAno->x = Frame[n].x;
  3276. SelAno->y = Frame[n].y + Frame[n].h + FixFontHeight;
  3277. SelAno->edco = 6;
  3278. SelAno->w = SelAno->edco * FixFontWidth + 2;
  3279. SelAno->xt = SelAno->x + (SelAno->edco + 1) * FixFontWidth;
  3280. SelAno->yt = SelAno->y + FixFont->ascent + 1;
  3281. strcpy(SelAno->teva," 0");
  3282. Button[4].y = SelAno->y - FixFontHeight/2; // Clear orography button
  3283. Button[4].x = Frame[n].x + Frame[n].w - 6 * FixFontWidth;
  3284. // T21 mode selector
  3285. ++n;
  3286. Frame[n].x = sfbox_x;
  3287. Frame[n].y = Frame[n-1].y + Frame[n-1].h + 4 * FixFontHeight;
  3288. moselw = sfbox_w;
  3289. if (SmallScreen)
  3290. {
  3291. moselw = WinYSize - Frame[n].y - FixFontHeight - FixFontHeight/2;
  3292. Frame[n].x += (sfbox_x - moselw) / 2;
  3293. }
  3294. Frame[n].w = moselw;
  3295. Frame[n].h = moselw;
  3296. Frame[n].b = BlackPix;
  3297. Frame[n].f = WhitePix;
  3298. strcpy(Frame[n].t[0],"Spherical Harmonics mode selector");
  3299. strcpy(Frame[n].t[1],"");
  3300. strcpy(Frame[n].t[2],"");
  3301. Frame[n].Action = ToggleMode;
  3302. sfbox_b = Frame[n].y + Frame[n].h - Frame[0].y + 2 * FixFontHeight + FixFontHeight/2;
  3303. Frames = n + 1;
  3304. }
  3305. void InitButtons(void)
  3306. {
  3307. int n;
  3308. bubox_w = 64;
  3309. bubox_h = 64;
  3310. bubox_x = WINDOW_WIDTH - 4 * 64 - 6 * FixFontWidth;
  3311. bubox_y = 8;
  3312. // Preprocess
  3313. n = 0;
  3314. Button[n].x = bubox_x;
  3315. Button[n].y = bubox_y;
  3316. Button[n].w = bubox_w;
  3317. Button[n].h = bubox_h;
  3318. Button[n].b = Yellow.pixel;
  3319. Button[n].f = BlackPix;
  3320. strcpy(Button[n].t[0],"Pre-");
  3321. strcpy(Button[n].t[1],"pro-");
  3322. strcpy(Button[n].t[2],"cess");
  3323. Button[n].Action = PreProcess;
  3324. // Save & Exit
  3325. ++n;
  3326. Button[n].x = Button[n-1].x + Button[n-1].w + FixFontWidth;
  3327. Button[n].y = Button[n-1].y;
  3328. Button[n].w = bubox_w;
  3329. Button[n].h = bubox_h;
  3330. Button[n].b = DarkGreen.pixel;
  3331. Button[n].f = WhitePix;
  3332. strcpy(Button[n].t[0],"Save");
  3333. strcpy(Button[n].t[1],"&");
  3334. strcpy(Button[n].t[2],"Exit");
  3335. Button[n].Action = SaveExit;
  3336. // Save & Run
  3337. ++n;
  3338. Button[n].x = Button[n-1].x + Button[n-1].w + FixFontWidth;
  3339. Button[n].y = Button[n-1].y;
  3340. Button[n].w = bubox_w;
  3341. Button[n].h = bubox_h;
  3342. Button[n].b = DarkBlue.pixel;
  3343. Button[n].f = WhitePix;
  3344. strcpy(Button[n].t[0],"Save");
  3345. strcpy(Button[n].t[1],"&");
  3346. strcpy(Button[n].t[2],"Run");
  3347. Button[n].Action = SaveRun;
  3348. // Abort
  3349. ++n;
  3350. Button[n].x = Button[n-1].x + Button[n-1].w + FixFontWidth;
  3351. Button[n].y = Button[n-1].y;
  3352. Button[n].w = bubox_w;
  3353. Button[n].h = bubox_h;
  3354. Button[n].b = Red.pixel;
  3355. Button[n].f = WhitePix;
  3356. strcpy(Button[n].t[1],"Abort");
  3357. Button[n].Action = Abort;
  3358. // Clear orography
  3359. ++n;
  3360. // Button[n].x = Button[n-1].x;
  3361. // Button[n].y = Button[n-1].y; // set in InitFrames
  3362. Button[n].w = 6 * FixFontWidth;
  3363. Button[n].h = 2 * FixFontHeight;
  3364. Button[n].b = DarkBlue.pixel;
  3365. Button[n].f = Yellow.pixel;
  3366. strcpy(Button[n].t[1],"Clear");
  3367. Button[n].Action = ClearOro;
  3368. }
  3369. /* ================== */
  3370. /* CreateModeSelector */
  3371. /* ================== */
  3372. void CreateModeSelector(int k)
  3373. {
  3374. int i,j,m,n;
  3375. int x,y,len,width,height;
  3376. int r,dx,dy,xp,yp;
  3377. char Text[20];
  3378. pix = Frame[k].pixmap = XCreatePixmap(display,Cow,Frame[k].w,Frame[k].h,ScreenD);
  3379. if (!Ampli) Ampli = malloc(DimSE * sizeof(int));
  3380. if (!ModeX) ModeX = malloc(DimSE * sizeof(int));
  3381. if (!ModeY) ModeY = malloc(DimSE * sizeof(int));
  3382. if (!ModeM) ModeM = malloc(DimSE * sizeof(int));
  3383. if (!ModeN) ModeN = malloc(DimSE * sizeof(int));
  3384. dxsh = Frame[k].w / (DimTr + 3);
  3385. dxs2 = dxsh / 2;
  3386. dx = dxsh;
  3387. r = (dx+dx) / 3;
  3388. ModeRadiusSq = r*r;
  3389. for (m=0,i=0 ; m <= DimTr ; ++m)
  3390. {
  3391. y = ModFontHeight + 4 + m * dx;
  3392. for (n=m ; n <= DimTr ; ++n,++i)
  3393. {
  3394. x = dx/2 + n * dx;
  3395. Ampli[i] = 1;
  3396. ModeX[i] = x + dx/2;
  3397. ModeY[i] = y + dx/2;
  3398. ModeM[i] = m;
  3399. ModeN[i] = n;
  3400. }
  3401. }
  3402. i = DimSH; // Button for all modes on
  3403. x = dx - ModFontWidth/2 + 1;
  3404. y = ModFontHeight + ModFont->descent + 2 + 20 * dx;
  3405. Ampli[i] = 0;
  3406. ModeX[i] = x + dx/2;
  3407. ModeY[i] = y + dx/2;
  3408. i++; // Button for all modes off
  3409. y += dx;
  3410. Ampli[i] = 0;
  3411. ModeX[i] = x + dx/2;
  3412. ModeY[i] = y + dx/2;
  3413. /* Draw mode legend */
  3414. dx = dxsh;
  3415. XSetForeground(display,gc,Red.pixel);
  3416. XSetBackground(display,gc,BlackPix);
  3417. for (i=0 ; i < 21 ; i+=2)
  3418. {
  3419. sprintf(Text,"%d",i);
  3420. len = strlen(Text);
  3421. width = XTextWidth(ModFont,Text,len);
  3422. height = ModFont->ascent + ModFont->descent;
  3423. xp = dx + i * dx - width/2 - ModFontWidth/2 + 1;
  3424. yp = ModFontHeight;
  3425. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  3426. }
  3427. XSetForeground(display,gc,Blue.pixel);
  3428. for (i=0 ; i < 21 ; i+=2)
  3429. {
  3430. sprintf(Text,"%d",i);
  3431. len = strlen(Text);
  3432. width = XTextWidth(ModFont,Text,len);
  3433. height = ModFont->ascent + ModFont->descent;
  3434. xp = Frame[k].x + Frame[k].w - width - 2;
  3435. yp = 2 * ModFontHeight + i * dx;
  3436. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  3437. }
  3438. }
  3439. void ShowButtons(void)
  3440. {
  3441. int i,k,l,x,y,d;
  3442. XSetFont(display, gc, FixFont->fid);
  3443. for (k=0 ; k < DIMBUTTON ; ++k)
  3444. {
  3445. if (k == 4 && SelAno->hide) continue;
  3446. d = Button[k].h / 4;
  3447. XSetForeground(display,gc,Button[k].b);
  3448. XFillRectangle(display,Cow,gc,Button[k].x,Button[k].y,Button[k].w,Button[k].h);
  3449. XSetForeground(display,gc,Button[k].f);
  3450. XSetBackground(display,gc,Button[k].b);
  3451. for (i=0 ; i < 3 ; ++i)
  3452. if (Button[k].t[i][0])
  3453. {
  3454. l = strlen(Button[k].t[i]);
  3455. x = Button[k].x + (Button[k].w - l * FixFontWidth) / 2;
  3456. y = Button[k].y + i * d + FixFont->ascent + d/2;
  3457. XDrawImageString(display,Cow,gc,x,y,Button[k].t[i],l);
  3458. }
  3459. }
  3460. }
  3461. int AllocateColorCells(struct ColorStrip cs[])
  3462. {
  3463. int i = 0;
  3464. XColor xcolor1,xcolor2;
  3465. while (cs[i].Name)
  3466. {
  3467. XAllocNamedColor(display,colormap,cs[i].Name,&xcolor1,&xcolor2);
  3468. cs[i].pixel = xcolor1.pixel;
  3469. ++i;
  3470. }
  3471. return i;
  3472. }
  3473. void InitColors(void)
  3474. {
  3475. /*
  3476. Visual *visual;
  3477. int count,st;
  3478. XStandardColormap *best_map_info;
  3479. XStandardColormap bmi[8];
  3480. visual = DefaultVisual(display,0);
  3481. memset(bmi,0,8*sizeof(XStandardColormap));
  3482. best_map_info = bmi;
  3483. count = 0;
  3484. st = XGetRGBColormaps(display,RootWindow(display,0),&best_map_info,&count,XA_RGB_DEFAULT_MAP);
  3485. {
  3486. printf("status = %d count=%d\n",st,count);
  3487. printf("colormap = %p\n",bmi[0].colormap);
  3488. printf("mapping = %x/%x/%x\n",bmi[0].red_max,bmi[0].green_max,bmi[0].blue_max);
  3489. }
  3490. */
  3491. // colormap = best_map_info->colormap;
  3492. AllocateColorCells(OroStrip);
  3493. AllocateColorCells(OroMarsStrip);
  3494. AllocateColorCells(GibbStrip);
  3495. AllocateColorCells(TStrip);
  3496. XAllocNamedColor(display,colormap,"red" ,&Red ,&Dummy);
  3497. XAllocNamedColor(display,colormap,"green" ,&Green ,&Dummy);
  3498. XAllocNamedColor(display,colormap,"blue" ,&Blue ,&Dummy);
  3499. XAllocNamedColor(display,colormap,"grey" ,&Grey ,&Dummy);
  3500. XAllocNamedColor(display,colormap,"hot pink" ,&LightRed ,&Dummy);
  3501. XAllocNamedColor(display,colormap,"dark red" ,&DarkRed ,&Dummy);
  3502. XAllocNamedColor(display,colormap,"light blue" ,&LightBlue ,&Dummy);
  3503. XAllocNamedColor(display,colormap,"dark blue" ,&DarkBlue ,&Dummy);
  3504. XAllocNamedColor(display,colormap,"light green",&LightGreen,&Dummy);
  3505. XAllocNamedColor(display,colormap,"dark green" ,&DarkGreen ,&Dummy);
  3506. XAllocNamedColor(display,colormap,"yellow" ,&Yellow ,&Dummy);
  3507. XAllocNamedColor(display,colormap,"cyan" ,&Cyan ,&Dummy);
  3508. TextC = Yellow.pixel;
  3509. HeadC = Cyan.pixel;
  3510. }
  3511. struct BMIstruct
  3512. {
  3513. int Size;
  3514. int Width;
  3515. int Height;
  3516. short Planes;
  3517. short Count;
  3518. int Compression;
  3519. int SizeImage;
  3520. int XPelsPerMeter;
  3521. int YPelsPerMeter;
  3522. int ClrUsed;
  3523. int ClrImportant;
  3524. };
  3525. // Convert an RGB value to an X11 Pixel
  3526. unsigned long create_pixel(long red, long green, long blue)
  3527. {
  3528. if (ScreenD == 24) // 24 bit true color
  3529. {
  3530. return blue | green << 8 | red << 16;
  3531. }
  3532. else if (ScreenD == 16) // 16 bit RGB 565
  3533. {
  3534. return blue >> 3 | (green >> 2) << 5 | (red >> 3) << 11;
  3535. }
  3536. else return 0;
  3537. }
  3538. void SwapIEEE16(char W[2])
  3539. {
  3540. char B;
  3541. B = W[0]; W[0] = W[1]; W[1] = B;
  3542. }
  3543. void SwapIEEE32(char W[4])
  3544. {
  3545. char B;
  3546. B = W[0]; W[0] = W[3]; W[3] = B;
  3547. B = W[1]; W[1] = W[2]; W[2] = B;
  3548. }
  3549. int ReadINT(FILE *fpi)
  3550. {
  3551. int k;
  3552. fread(&k,sizeof(k),1,fpi);
  3553. if (BigEndian) SwapIEEE32((char *)&k);
  3554. return k;
  3555. }
  3556. short ReadSHORT(FILE *fpi)
  3557. {
  3558. short k;
  3559. fread(&k,sizeof(k),1,fpi);
  3560. if (BigEndian) SwapIEEE16((char *)&k);
  3561. return k;
  3562. }
  3563. struct BMIstruct ImageBMI;
  3564. void ReadImage(struct MapImageStruct *ei, char *filename)
  3565. {
  3566. char ch;
  3567. int i,n,x,y,z;
  3568. long r,g,b;
  3569. int FileSize;
  3570. int Reserved;
  3571. int OffBits;
  3572. int PicBytes;
  3573. int ImgBytes;
  3574. int PadWidth;
  3575. int PadBytes;
  3576. int bpp;
  3577. int byr;
  3578. FILE *fp;
  3579. unsigned char *BuffImageData;
  3580. if (!(fp = fopen(filename,"r"))) return;
  3581. ch = fgetc(fp);
  3582. if (ch != 'B') return;
  3583. ch = fgetc(fp);
  3584. if (ch != 'M') return;
  3585. FileSize = ReadINT(fp);
  3586. Reserved = ReadINT(fp);
  3587. OffBits = ReadINT(fp);
  3588. if (Debug)
  3589. {
  3590. printf("Properties of %s:\n",filename);
  3591. printf("FileSize = %d\n",FileSize);
  3592. printf("FileOffset = %d\n",OffBits);
  3593. }
  3594. ImageBMI.Size = ReadINT(fp);
  3595. ImageBMI.Width = ReadINT(fp);
  3596. ImageBMI.Height = ReadINT(fp);
  3597. ImageBMI.Planes = ReadSHORT(fp);
  3598. ImageBMI.Count = ReadSHORT(fp);
  3599. ImageBMI.Compression = ReadINT(fp);
  3600. ImageBMI.SizeImage = ReadINT(fp);
  3601. ImageBMI.XPelsPerMeter = ReadINT(fp);
  3602. ImageBMI.YPelsPerMeter = ReadINT(fp);
  3603. ImageBMI.ClrUsed = ReadINT(fp);
  3604. ImageBMI.ClrImportant = ReadINT(fp);
  3605. PadWidth = (ImageBMI.Width + 7) & 0xFFF8;
  3606. bpp = ImageBMI.Count >> 3;
  3607. PadBytes = (4 - ((ImageBMI.Width * bpp) % 4)) % 4 ;
  3608. if (Debug)
  3609. {
  3610. printf("BMI Size = %d\n",ImageBMI.Size);
  3611. printf("BMI Width = %d\n",ImageBMI.Width);
  3612. printf("BMI Height = %d\n",ImageBMI.Height);
  3613. printf("BMI Planes = %d\n",ImageBMI.Planes);
  3614. printf("BMI Count = %d\n",ImageBMI.Count);
  3615. printf("BMI ClrUsed = %d\n",ImageBMI.ClrUsed);
  3616. printf("BMI ClrImpo = %d\n",ImageBMI.ClrImportant);
  3617. printf("Pad Bytes = %d\n",PadBytes);
  3618. printf("Pad Width = %d\n",PadWidth);
  3619. }
  3620. PicBytes = (bpp * ImageBMI.Width + PadBytes) * ImageBMI.Height;
  3621. ImgBytes = 4 * PadWidth * ImageBMI.Height;
  3622. ei->d = calloc(ImgBytes,1);
  3623. BuffImageData = malloc(PicBytes);
  3624. fseek(fp,OffBits,SEEK_SET);
  3625. n = fread(BuffImageData,1,PicBytes,fp);
  3626. fclose(fp);
  3627. if (Debug)
  3628. {
  3629. printf("Size Bytes = %d\n",PicBytes);
  3630. printf("Read Bytes = %d\n",n);
  3631. }
  3632. ei->X = XCreateImage(display,CopyFromParent,ScreenD,ZPixmap,0,
  3633. ei->d,PadWidth,ImageBMI.Height,8,0);
  3634. for (y = 0 ; y < ImageBMI.Height ; ++y)
  3635. {
  3636. for (x = 0 ; x < ImageBMI.Width ; ++x)
  3637. {
  3638. i = bpp * x + (ImageBMI.Height - 1 - y) * (ImageBMI.Width*bpp+PadBytes);
  3639. b = BuffImageData[i ];
  3640. g = BuffImageData[i+1];
  3641. r = BuffImageData[i+2];
  3642. XPutPixel(ei->X, x, y, create_pixel(r,g,b));
  3643. }
  3644. }
  3645. free(BuffImageData);
  3646. ei->w = ImageBMI.Width;
  3647. ei->h = ImageBMI.Height;
  3648. }
  3649. void ReadLogo(int logo, char *filename)
  3650. {
  3651. struct MapImageStruct MapI;
  3652. ReadImage(&MapI,filename);
  3653. Logo[logo].w = MapI.w;
  3654. Logo[logo].h = MapI.h;
  3655. Logo[logo].X = MapI.X;
  3656. if (logo)
  3657. {
  3658. Logo[logo].x = Logo[logo-1].x + Logo[logo-1].w;
  3659. Logo[logo].y = Logo[logo-1].y;
  3660. }
  3661. }
  3662. void ShowCopyright(void)
  3663. {
  3664. int x,y;
  3665. x = CowSizeHints.min_width - 18.5 * ModFontWidth;
  3666. y = CowSizeHints.min_height - ModFontHeight/2;
  3667. XSetFont(display, gc, ModFont->fid);
  3668. XSetForeground(display,gc,Grey.pixel);
  3669. XSetBackground(display,gc,BlackPix);
  3670. XDrawImageString(display,Cow,gc,x,y,"Image Credit: NASA",18);
  3671. }
  3672. int redaco;
  3673. int RedrawControlWindow(void)
  3674. {
  3675. int i,l,x,y;
  3676. double xrf,yrf;
  3677. struct SelStruct *Sel;
  3678. XWindowAttributes CurAtt;
  3679. if (Debug) printf("Redraw %d\n",redaco++);
  3680. XGetWindowAttributes(display,Cow,&CurAtt);
  3681. WinXSize = CurAtt.width;
  3682. WinYSize = CurAtt.height;
  3683. XSetWindowBackground(display,Cow,WinBG);
  3684. XClearWindow(display,Cow);
  3685. // Paint some stars on the sky
  3686. xrf = (double)WinXSize / pow(2.0,31),
  3687. yrf = (double)WinYSize / pow(2.0,31);
  3688. XSetForeground(display,gc,WhitePix);
  3689. srandom(Seed);
  3690. for (y = 0 ; y < ((WinXSize * WinYSize) >> 8) ; ++y)
  3691. XDrawPoint(display,Cow,gc,xrf*random(),yrf*random());
  3692. XSetForeground(display,gc,BlackPix);
  3693. XSetBackground(display,gc,WhitePix);
  3694. for (Sel = &SelStart ; Sel ; Sel = Sel->Next) ShowSelection(Sel);
  3695. if (CursorSel) CursorOn();
  3696. ShowOrography();
  3697. ShowFrame1();
  3698. ShowButtons();
  3699. for (l=0 ; l < Logos ; ++l)
  3700. {
  3701. XPutImage(display,Cow,gc,Logo[l].X,0,0,Logo[l].x,Logo[l].y,Logo[l].w,Logo[l].h);
  3702. }
  3703. if (Model == PLASIM) ShowCopyright();
  3704. XSync(display,0);
  3705. return 0;
  3706. }
  3707. void ChangeResolution(int NewRes)
  3708. {
  3709. int i;
  3710. struct SelStruct *Sel;
  3711. for (i=RES_T21 , Sel = SelRes ; i <= RES_T42; ++i , Sel = Sel->Next)
  3712. {
  3713. if (i == NewRes) Sel->iv = 1;
  3714. else Sel->iv = 0;
  3715. }
  3716. Resolution = NewRes;
  3717. }
  3718. void UpdateResolution(void)
  3719. {
  3720. int i;
  3721. struct SelStruct *Sel;
  3722. if (SelRes)
  3723. for (i=RES_T21 , Sel = SelRes ; i <= RES_T42; ++i , Sel = Sel->Next)
  3724. {
  3725. if (Sel && Sel->iv == 1) Resolution = i;
  3726. }
  3727. }
  3728. char *vcn[6] =
  3729. {
  3730. "StaticGray",
  3731. "GrayScale",
  3732. "StaticColor",
  3733. "PseudoColor",
  3734. "TrueColor",
  3735. "DirectColor"
  3736. };
  3737. void InitGUI(void)
  3738. {
  3739. int argc = 1;
  3740. int i,j,k;
  3741. char *WinconTitle1 = {"MoSt - Model Suite (17) - University of Hamburg"};
  3742. unsigned long valuemask = 0; /* ignore XGCvalues and use defaults */
  3743. FILE *ftp;
  3744. FILE *xpp;
  3745. XVisualInfo vinfo;
  3746. XGCValues values;
  3747. XEvent Event;
  3748. XWindowAttributes CurAtt;
  3749. if ((display=XOpenDisplay(display_name)) == NULL)
  3750. {
  3751. fprintf(stderr,"%s: cannot connect to X server %s\n",
  3752. progname, XDisplayName(display_name));
  3753. exit(1);
  3754. }
  3755. ScreenN = DefaultScreen(display);
  3756. ScreenD = XDefaultDepth(display,ScreenN);
  3757. ScreenW = DisplayWidth (display,ScreenN);
  3758. ScreenH = DisplayHeight(display,ScreenN);
  3759. if (ScreenHeight) ScreenH = ScreenHeight;
  3760. if (Debug)
  3761. {
  3762. i=5;
  3763. while (!XMatchVisualInfo(display,ScreenN,ScreenD,i--,&vinfo));
  3764. printf("Found visual %s at depth %d\n",vcn[++i],ScreenD);
  3765. printf("Red mask = %8lx\n",vinfo.red_mask);
  3766. printf("Green mask = %8lx\n",vinfo.green_mask);
  3767. printf("Blue mask = %8lx\n",vinfo.blue_mask);
  3768. }
  3769. BlackPix = BlackPixel(display,ScreenN);
  3770. WhitePix = WhitePixel(display,ScreenN);
  3771. SmallScreen = ScreenH < 768;
  3772. wm_hints.initial_state = NormalState;
  3773. wm_hints.input = True;
  3774. wm_hints.flags = StateHint | InputHint;
  3775. class_hints.res_name = progname;
  3776. class_hints.res_class = "MOST";
  3777. Delwin = XInternAtom(display,"WM_DELETE_WINDOW",0);
  3778. LoadFonts();
  3779. /* Setup control window */
  3780. CowSizeHints.flags = PPosition | PSize | PMinSize | PMaxSize;
  3781. CowSizeHints.min_width = MIN(WINDOW_WIDTH,ScreenW);
  3782. CowSizeHints.min_height = MIN(740,ScreenH);
  3783. CowSizeHints.max_width = CowSizeHints.min_width;
  3784. CowSizeHints.max_height = CowSizeHints.min_height;
  3785. Cow = XCreateSimpleWindow(display,RootWindow(display,ScreenN),
  3786. 0,0,
  3787. CowSizeHints.max_width,CowSizeHints.max_height,
  3788. 4,BlackPix,WhitePix);
  3789. XStringListToTextProperty(&WinconTitle1,1,&WinconName1);
  3790. XSetWMProtocols(display,Cow,&Delwin,1);
  3791. XSetWMProperties(display,Cow,&WinconName1,NULL,
  3792. NULL,0,&CowSizeHints,&wm_hints,&class_hints);
  3793. XSelectInput(display,Cow,ButtonPressMask | ButtonReleaseMask |
  3794. PointerMotionMask | KeyPressMask | ExposureMask);
  3795. XMapWindow(display,Cow);
  3796. XGetWindowAttributes(display,Cow,&CurAtt);
  3797. WinXSize = CurAtt.width;
  3798. WinYSize = CurAtt.height;
  3799. /* Prepare GC */
  3800. gc = XCreateGC(display, Cow, valuemask, &values);
  3801. XSetFont(display, gc, FixFont->fid);
  3802. colormap = XDefaultColormap(display,ScreenN);
  3803. XSetForeground(display,gc,BlackPix);
  3804. XSetBackground(display,gc,WhitePix);
  3805. Mok = XGetModifierMapping(display);
  3806. InitColors();
  3807. /* Look for Num_Lock key */
  3808. for (j=0 ; j < 8 ; ++j)
  3809. {
  3810. k = Mok->modifiermap[j * Mok->max_keypermod];
  3811. if (XkbKeycodeToKeysym(display,k,0,0) == XK_Num_Lock ) NumLockMask = 1 << j;
  3812. if (XkbKeycodeToKeysym(display,k,0,0) == XK_Mode_switch) ModeSwitchMask = 1 << j;
  3813. }
  3814. if (Debug) printf("NumLockMask = %02X\n",NumLockMask );
  3815. if (Debug) printf("ModeSwitchMask = %02X\n",ModeSwitchMask);
  3816. if (Debug)
  3817. for (j=0 ; j < 8 ; ++j)
  3818. {
  3819. printf("Mok [%d] %d:",Mok->max_keypermod,j);
  3820. for (i=0 ; i < Mok->max_keypermod ; ++i)
  3821. {
  3822. k = Mok->modifiermap[i+j*Mok->max_keypermod];
  3823. if (XkbKeycodeToKeysym(display,k,0,0) != NoSymbol)
  3824. printf(" %4x %-16.16s ",k,XKeysymToString(XkbKeycodeToKeysym(display,k,0,0)));
  3825. }
  3826. printf("\n");
  3827. }
  3828. XDisplayKeycodes(display,&EdiFirstKey,&EdiLastKey);
  3829. EdiKeymap = XGetKeyboardMapping(display,EdiFirstKey,EdiLastKey-EdiFirstKey+1,&EdiSymsPerKey);
  3830. /* Sun keymaps don't have lower case entries */
  3831. for (j=EdiFirstKey ; j <= EdiLastKey ; ++j)
  3832. {
  3833. k = (j - EdiFirstKey) * EdiSymsPerKey;
  3834. if (EdiKeymap[k] >= 'A' && EdiKeymap[k] < 0xE0 && EdiKeymap[k+1] == 0)
  3835. {
  3836. EdiKeymap[k+1] = EdiKeymap[k];
  3837. EdiKeymap[k] += 0x20;
  3838. }
  3839. }
  3840. //FL0318 xpp = fopen("Beginner","r"); // Expert mode ?
  3841. xpp = fopen("Expert","r"); // Expert mode ?
  3842. if (xpp)
  3843. {
  3844. //FL0318 Expert = 0;
  3845. Expert = 1;
  3846. fclose(xpp);
  3847. }
  3848. xpp = fopen("cat","r"); // Cat enabled
  3849. if (xpp)
  3850. {
  3851. CatEnabled = 1;
  3852. fclose(xpp);
  3853. }
  3854. xpp = fopen("sam","r"); // Sam enabled
  3855. if (xpp)
  3856. {
  3857. SamEnabled = 1;
  3858. fclose(xpp);
  3859. }
  3860. xpp = fopen("lsg/src/lsgmod.f90","r"); // LSG there ?
  3861. //FL0318 if (xpp)
  3862. if (xpp && Expert)
  3863. {
  3864. LsgEnabled = 1;
  3865. fclose(xpp);
  3866. }
  3867. xpp = fopen("puma/src/mpimod_multi.f90","r"); // Multirun module there ?
  3868. if (xpp)
  3869. {
  3870. MultirunEnabled = 1;
  3871. fclose(xpp);
  3872. }
  3873. // Read name of MPI execute command
  3874. xpp = fopen("most_compiler_mpi","r"); // MPI installed ?
  3875. if (xpp)
  3876. {
  3877. fgets(Buffer,LINEMAX,xpp);
  3878. if (Buffer[strlen(Buffer)-1] == 10) Buffer[strlen(Buffer)-1] = 0;
  3879. if (Buffer[strlen(Buffer)-1] == 13) Buffer[strlen(Buffer)-1] = 0;
  3880. if (!strncmp(Buffer,"MPI_RUN=",8)) strcpy(mpirun,Buffer+8);
  3881. fclose(xpp);
  3882. }
  3883. InitLogo();
  3884. ReadLogo(0,"images/KC-Logo_RGB.bmp");
  3885. ReadLogo(1,"images/puma.bmp");
  3886. ReadLogo(2,"images/cat.bmp");
  3887. ReadLogo(3,"images/plasim.bmp");
  3888. ReadImage(&MapHRE,"images/earth.bmp");
  3889. ReadImage(&MapHRM,"images/mars.bmp");
  3890. ReadImage(&MapLRK,"images/Kepler-16.bmp");
  3891. ReadImage(&MapLRL,"images/habit360x180.bmp");
  3892. opbox_y = 8 + 64 + 2 * FixFontHeight;
  3893. InitSelections();
  3894. InitNamelist();
  3895. ChangeModel(PLASIM);
  3896. NamelistSelector(PLASIM);
  3897. ChangeModel(CAT);
  3898. NamelistSelector(CAT);
  3899. ChangeModel(SAM);
  3900. NamelistSelector(SAM);
  3901. ChangeModel(PUMA);
  3902. NamelistSelector(PUMA);
  3903. if (ReadSettings(cfg_file))
  3904. {
  3905. UpdateResolution();
  3906. UpdateSelections(&SelStart);
  3907. }
  3908. InitFrames();
  3909. CalcFrame(nlat);
  3910. CreateModeSelector(1);
  3911. InitButtons();
  3912. MapLRE.w = Frame[1].w;
  3913. MapLRE.h = Frame[1].h;
  3914. MapLRK.w = Frame[1].w;
  3915. MapLRK.h = Frame[1].h;
  3916. MapLRM.w = Frame[1].w;
  3917. MapLRM.h = Frame[1].h;
  3918. MapLRL.w = Frame[0].w;
  3919. MapLRL.h = Frame[0].h;
  3920. MapLRM.l = -90;
  3921. AzimuthalImage(&MapHRE,&MapLRE);
  3922. AzimuthalImage(&MapHRM,&MapLRM);
  3923. RedrawControlWindow();
  3924. }
  3925. int HitBox(struct SelStruct *Sel)
  3926. {
  3927. return (
  3928. (WinEvent.xbutton.button == Button1) &&
  3929. (WinEvent.xbutton.x >= Sel->x) &&
  3930. (WinEvent.xbutton.x < Sel->x + Sel->w) &&
  3931. (WinEvent.xbutton.y >= Sel->y) &&
  3932. (WinEvent.xbutton.y < Sel->y + Sel->h));
  3933. }
  3934. int HitButton(int k)
  3935. {
  3936. return (
  3937. (WinEvent.xbutton.button == Button1) &&
  3938. (WinEvent.xbutton.x >= Button[k].x) &&
  3939. (WinEvent.xbutton.x < Button[k].x + Button[k].w) &&
  3940. (WinEvent.xbutton.y >= Button[k].y) &&
  3941. (WinEvent.xbutton.y < Button[k].y + Button[k].h));
  3942. }
  3943. int HitFrame(int k)
  3944. {
  3945. return (
  3946. (WinEvent.xbutton.x >= Frame[k].x) &&
  3947. (WinEvent.xbutton.x < Frame[k].x + Frame[k].w) &&
  3948. (WinEvent.xbutton.y >= Frame[k].y) &&
  3949. (WinEvent.xbutton.y < Frame[k].y + Frame[k].h));
  3950. }
  3951. int OnFrame(int i)
  3952. {
  3953. if (HitFrame(i))
  3954. {
  3955. FrameNo = i;
  3956. Frame[i].Action();
  3957. return 1;
  3958. }
  3959. return 0;
  3960. }
  3961. void OnMouseClick(void)
  3962. {
  3963. int i,j,cp;
  3964. struct SelStruct *Sel;
  3965. /* Check Action Buttons */
  3966. for (i=0 ; i < DIMBUTTON ; ++i)
  3967. {
  3968. if (HitButton(i))
  3969. {
  3970. Button[i].Action();
  3971. return;
  3972. }
  3973. }
  3974. /* Check for model switch */
  3975. for (i = PUMA , Sel = SelMod ; i < MODELS ; ++i , Sel = Sel->Next)
  3976. {
  3977. if (HitBox(Sel) && Model != i)
  3978. {
  3979. if (i == CAT && !CatEnabled) return; // Hide CAT
  3980. if (Debug) printf("Change model from %d to %d\n",Model,i);
  3981. ChangeModel(i);
  3982. CalcFrame(Latitudes);
  3983. return;
  3984. }
  3985. }
  3986. /* Continue with all other boxes */
  3987. for ( ; Sel ; Sel = Sel->Next)
  3988. {
  3989. if (Sel->type == SEL_CHECK && HitBox(Sel) && !Sel->no)
  3990. {
  3991. cp = -1;
  3992. for (i=0 ; i < PLANETS ; ++i) if (Sel == SelPlanet[i]) cp = i;
  3993. if (cp >= 0 && Planet != cp) ChangePlanet(cp);
  3994. else
  3995. {
  3996. Sel->iv = !Sel->iv;
  3997. if (Sel->piv) *Sel->piv = Sel->iv;
  3998. }
  3999. }
  4000. else if ((Sel->type == SEL_INT || Sel->type == SEL_REAL) && HitBox(Sel))
  4001. {
  4002. // printf("Target is <%s>\n",Sel->text);
  4003. if (Sel != CursorSel) FinishLine();
  4004. CursorSel = Sel;
  4005. CursorCol = (WinEvent.xbutton.x - Sel->x) / FixFontWidth;
  4006. if (CursorCol > Sel->edco-1) CursorCol = Sel->edco-1;
  4007. CursorOn();
  4008. }
  4009. }
  4010. /* Allow only one horizontal resolution */
  4011. if (SelRes)
  4012. {
  4013. for (i = RES_T21 , Sel = SelRes ; i <= RES_T42 ; ++i , Sel = Sel->Next)
  4014. if (HitBox(Sel))
  4015. {
  4016. ChangeResolution(i);
  4017. return;
  4018. }
  4019. }
  4020. }
  4021. int EdiDecodeKey(void)
  4022. {
  4023. int ShiftStatus;
  4024. int ShiftIndex;
  4025. int KeyCode;
  4026. int KeyIndex;
  4027. ShiftStatus = WinEvent.xkey.state;
  4028. KeyIndex = (WinEvent.xkey.keycode-EdiFirstKey) * EdiSymsPerKey;
  4029. KeyCode = EdiKeymap[KeyIndex]; // Code with no modifiers
  4030. if (ShiftStatus & 0x04) // Control
  4031. {
  4032. if (KeyCode >= 0x40 && KeyCode < 0x80) KeyCode &= 0x1F;
  4033. }
  4034. else if (ShiftStatus & NumLockMask &&
  4035. EdiKeymap[KeyIndex+1] >= XK_KP_Separator && // Numlock
  4036. EdiKeymap[KeyIndex+1] <= XK_KP_9) // PC Keypad
  4037. KeyCode = EdiKeymap[KeyIndex+1] - XK_KP_Space;
  4038. else if (ShiftStatus & NumLockMask &&
  4039. EdiKeymap[KeyIndex+2] >= XK_KP_Separator && // Numlock
  4040. EdiKeymap[KeyIndex+2] <= XK_KP_9) // SUN Keypad
  4041. KeyCode = EdiKeymap[KeyIndex+2] - XK_KP_Space;
  4042. else
  4043. {
  4044. if (ShiftStatus & ModeSwitchMask) ShiftIndex = 2; // Alt Gr
  4045. else ShiftIndex = ShiftStatus & 1; // Normal & Shift
  4046. if (ShiftIndex == 1 && KeyCode >= XK_F1 && KeyCode <= XK_F12) KeyCode += 12;
  4047. else KeyCode = EdiKeymap[KeyIndex + ShiftIndex];
  4048. }
  4049. if (ShiftStatus & 0x02) // Caps Lock
  4050. {
  4051. if (KeyCode >= 'a' && KeyCode <= 'z' ) KeyCode -= 0x20; // ASCII
  4052. if (KeyCode >= 0xE0 && KeyCode <= 0xFD) KeyCode -= 0x20; // Latin-1
  4053. }
  4054. if (KeyCode == XK_KP_Left ) KeyCode = XK_Left;
  4055. if (KeyCode == XK_KP_Right ) KeyCode = XK_Right;
  4056. if (KeyCode == XK_KP_Up ) KeyCode = XK_Up;
  4057. if (KeyCode == XK_KP_Down ) KeyCode = XK_Down;
  4058. if (KeyCode == XK_KP_Home ) KeyCode = XK_Home;
  4059. if (KeyCode == XK_KP_End ) KeyCode = XK_End ;
  4060. if (KeyCode == XK_KP_Page_Up ) KeyCode = XK_Page_Up;
  4061. if (KeyCode == XK_KP_Page_Down ) KeyCode = XK_Page_Down;
  4062. if (KeyCode == XK_KP_Enter ) KeyCode = XK_Return;
  4063. if (KeyCode == XK_KP_Add ) KeyCode = '+';
  4064. if (KeyCode == XK_KP_Subtract ) KeyCode = '-';
  4065. if (KeyCode == XK_KP_Multiply ) KeyCode = '*';
  4066. if (KeyCode == XK_KP_Divide ) KeyCode = '/';
  4067. if (Debug)
  4068. {
  4069. if (KeyCode >= ' ' && KeyCode <= 255) printf("'%c' ",KeyCode);
  4070. else printf("--- ");
  4071. printf("Key [%x] State [%2x]\n",WinEvent.xkey.keycode,WinEvent.xkey.state);
  4072. for (ShiftIndex = 0 ; ShiftIndex < EdiSymsPerKey ; ++ShiftIndex)
  4073. {
  4074. if (EdiKeymap[KeyIndex + ShiftIndex] && XKeysymToString(EdiKeymap[KeyIndex + ShiftIndex]))
  4075. {
  4076. printf(" %d: <%x> '%c' (%s)\n",
  4077. ShiftIndex,(unsigned int)EdiKeymap[KeyIndex + ShiftIndex],
  4078. (int)EdiKeymap[KeyIndex + ShiftIndex],
  4079. XKeysymToString(EdiKeymap[KeyIndex + ShiftIndex]));
  4080. }
  4081. }
  4082. printf("\n");
  4083. }
  4084. return KeyCode;
  4085. }
  4086. void EditUpLine(void)
  4087. {
  4088. FinishLine();
  4089. if (CursorSel == NULL || CursorSel->Prev == NULL) return;
  4090. if (CursorSel->Prev->type != SEL_INT && CursorSel->Prev->type != SEL_REAL) return;
  4091. CursorSel = CursorSel->Prev;
  4092. if (CursorCol > CursorSel->edco-1) CursorCol = CursorSel->edco-1;
  4093. }
  4094. void EditDownLine(void)
  4095. {
  4096. FinishLine();
  4097. if (CursorSel == NULL || CursorSel->Next == NULL) return;
  4098. if (CursorSel->Next->type != SEL_INT && CursorSel->Next->type != SEL_REAL) return;
  4099. CursorSel = CursorSel->Next;
  4100. if (CursorCol > CursorSel->edco-1) CursorCol = CursorSel->edco-1;
  4101. }
  4102. void EditReturnLine(void)
  4103. {
  4104. CursorCol = 0;
  4105. EditDownLine();
  4106. }
  4107. void EditLeftChar(void)
  4108. {
  4109. if (CursorCol > 0) --CursorCol;
  4110. }
  4111. void EditRightChar(void)
  4112. {
  4113. if (CursorCol < CursorSel->edco-1) ++CursorCol;
  4114. }
  4115. void EmitChar(int ch)
  4116. {
  4117. if (CursorSel)
  4118. {
  4119. CursorSel->teva[CursorCol] = ch;
  4120. EditRightChar();
  4121. }
  4122. } /* EmitChar */
  4123. void DeleteRightChar(void)
  4124. {
  4125. int i;
  4126. for (i=CursorCol ; i < CursorSel->edco-1 ; ++i)
  4127. CursorSel->teva[i] = CursorSel->teva[i+1];
  4128. CursorSel->teva[CursorSel->edco-1] = ' ';
  4129. }
  4130. void DeleteLeftChar(void)
  4131. {
  4132. if (CursorCol == 0) return;
  4133. EditLeftChar();
  4134. DeleteRightChar();
  4135. }
  4136. void InsertBlank(void)
  4137. {
  4138. int i;
  4139. for (i = CursorSel->edco-1 ; i > CursorCol ; --i)
  4140. CursorSel->teva[i] = CursorSel->teva[i-1];
  4141. CursorSel->teva[CursorCol] = ' ';
  4142. }
  4143. void ProcChar(int Key)
  4144. {
  4145. if (CursorSel)
  4146. switch (Key)
  4147. {
  4148. case XK_Up : EditUpLine(); break;
  4149. case XK_Left : EditLeftChar(); break;
  4150. case XK_Right : EditRightChar(); break;
  4151. case XK_Down : EditDownLine(); break;
  4152. case XK_Return : EditReturnLine(); break;
  4153. case XK_BackSpace: DeleteLeftChar(); break;
  4154. case XK_Delete : DeleteRightChar(); break;
  4155. case XK_Insert : InsertBlank(); break;
  4156. }
  4157. }
  4158. void LoopGUI(void)
  4159. {
  4160. int w;
  4161. while (1)
  4162. {
  4163. XNextEvent(display,&WinEvent);
  4164. switch (WinEvent.type)
  4165. {
  4166. case ClientMessage: return;
  4167. case MotionNotify : if (OnFrame(0)) ShowOrography();
  4168. if (Model == PUMA && OnFrame(1)) ShowModeSelector();
  4169. break;
  4170. case ButtonRelease: Button1Down = 0; if (OnFrame(0)) ShowOrography(); break;
  4171. case ButtonPress : Button1Down = 1;
  4172. if (!OnFrame(0) && !OnFrame(1)) OnMouseClick();
  4173. case Expose : RedrawControlWindow(); break;
  4174. case KeyPress : EdiKeyCode = EdiDecodeKey();
  4175. if (EdiKeyCode >= ' ' && EdiKeyCode < 256) EmitChar(EdiKeyCode);
  4176. else ProcChar(EdiKeyCode);
  4177. RedrawControlWindow();
  4178. break;
  4179. }
  4180. }
  4181. }
  4182. int CheckEndianess(void)
  4183. {
  4184. union EndianCheck
  4185. {
  4186. char b[sizeof(int)];
  4187. int i;
  4188. } ec;
  4189. ec.i = 8;
  4190. return (ec.b[0] == 0);
  4191. }
  4192. int main(int argc, char *argv[])
  4193. {
  4194. int ia;
  4195. for (ia = 1 ; ia < argc ; ++ia)
  4196. {
  4197. if (!strcmp(argv[ia],"-d")) Debug = 1;
  4198. else if (!strcmp(argv[ia],"-i")) cfg_file[0] = 0;
  4199. else if (!strcmp(argv[ia],"-h")) ScreenHeight = atoi(argv[++ia]);
  4200. else strncpy(cfg_file,argv[1],sizeof(cfg_file));
  4201. }
  4202. CurrentDate = time(NULL);
  4203. BigEndian = CheckEndianess();
  4204. InitGUI();
  4205. LoopGUI();
  4206. return 0;
  4207. }